Ошибка:
(Имя поля) не может быть нулевым
Однако все необходимое я ввел.
Код: Выделить всё
using FindWorkWeb.DataAccess.Data;
using FindWorkWeb.Models.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;
namespace FindWorkWeb.Areas.User.Controllers
{
[Area("User")]
public class OrderController : Controller
{
private readonly UserManager _userManager;
private readonly ApplicationDbContext _context;
public OrderController(UserManager userManager, ApplicationDbContext context)
{
_userManager = userManager;
_context = context;
}
public class InputModel()
{
[Display(Name = "Title")]
public string Title { get; set; }
[Display(Name = "Description")]
public string Description { get; set; }
[Display(Name = "ApproximatedPrice")]
public float ApproximatedPrice { get; set; }
[Display(Name = "City")]
public string City { get; set; }
}
public InputModel Input { get; set; }
[HttpGet]
[Authorize]
public IActionResult Create()
{
Input = new InputModel();
return View(Input);
}
[HttpPost]
public async Task Create(InputModel inputModel)
{
if (ModelState.IsValid)
{
var user = await _userManager.GetUserAsync(User);
var order = new Order
{
ApplicationUserId = user.Id,
Title = Input.Title,
Description = Input.Description,
ApproximatedPrice = Input.ApproximatedPrice,
City = Input.City,
Status = "Pending",
PaymentStatus = false
};
_context.Orders.Add(order);
await _context.SaveChangesAsync();
return RedirectToAction("Index", "Home");
}
return View();
}
}
}
Код: Выделить всё
@model FindWorkWeb.Areas.User.Controllers.OrderController.InputModel
@{
ViewData["Title"] = "Create Order";
}
Create Order
@* *@
@* *@
@* *@
@* *@
@section Scripts {
@{
await Html.RenderPartialAsync("_ValidationScriptsPartial");
}
}
Подробнее здесь: https://stackoverflow.com/questions/784 ... core-model
Мобильная версия