Ошибка в POST методе, передача null
Пишу на MVC, есть страница для создания клиента. Есть 2 модели: Client и City.
public class Client
{
[Key]
public int Id { get; set; }
public string Surname { get; set; }
public string Name { get; set; }
public string Patronymic { get; set; }
public string Telephone { get; set; }
public City City { get; set; }
public string Adress { get; set; }
[DisplayName ("Passport")]
public string SeriaNumberPassport { get; set; }
[DisplayName("Identifical Code")]
public string IdentificalCode { get; set; }
}
public class City
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
У меня есть так же DropDownList который при создании клиента выводит список доступных городов.
<div class="form-group" mb-3>
<label asp-for="City" class="control-label"></label>
@Html.DropDownList("Cities", (IEnumerable<SelectListItem>)ViewBag.Cities, "Select City", new { @class = "form-select" })
Но при нажатии кнопки создания вылетает ошибка, клиента невозможно создать, в дебаге City = null. не знаю как исправить. Ниже get и post методы Create
// GET: Clients/Create
public IActionResult Create()
{
if (_context.City != null) _cities = _context.City.ToList();
ViewBag.Cities = new SelectList(_cities, "Id", "Name");
return View();
}
// POST: Clients/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Surname,Name,Patronymic,Telephone,City,Adress,SeriaNumberPassport,IdentificalCode")]
Client client)
{
if (ModelState.IsValid)
{
_context.Add(client);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(client);
}