Если, например, у вас есть класс автомобиля, и этот класс автомобиля содержит тип автомобиля, который отражается таблицей в базе данных. Как мне получить MVC для привязки к типу автомобиля, когда я создаю новый автомобиль?Asp.net MVC Binding
public class Car
{
public string Name { get; set; }
public CarType Type { get; set; }
}
public class CarType
{
public int CarTypeId { get; set; }
public string Name { get; set; }
}
public class CarViewModel
{
public Car MyCar { get; set; }
public SelectList Types { get; set; }
}
..... inside my controller
public ActionResult Create()
{
return View(new CarViewModel { Car = new Car(), Types = new SelectList(repo.GetCarTypes(), "CarTypeId", "Name") });
}
[HttpPost]
public ActionResult Create(Car myCar)
{
//hopefully have bound correctly
repo.Add(car);
repo.Save();
}
Что мне нужно на мой взгляд, чтобы мой объект автомобиля мог правильно связываться?
Спасибо!