我对MVC相当陌生,我试图摆脱使用WebForms的形式,但有些事情让我感到困惑,因为我需要完成某些工作。MVC绑定模型到视图中的下拉列表
在我看来,“/areas/admin/cars/form.cshtml”我想用@ Html.Dropdownfor,传递的这是由CarManufacturer.cs模型中定义我的汽车制造商的列表。我该怎么做?
我使用NHibernate的 - 不知道如果让任何不同...
模型;
public class CarManufacturer
{
public virtual int Id { get; set; }
public virtual string Manufacturer { get; set; }
//***do i need the below line?***
public SelectList CarManufacturerList { get; set; }
}
public class CarManufacturerMap : ClassMapping<CarManufacturer>
{
public CarManufacturerMap()
{
Table("CarManufacturers");
Id(x => x.Id, x => x.Generator(Generators.Identity));
Property(x => x.Manufacturer, x => x.NotNullable(true));
}
}
的控制器调用形式是carController.cs
public ActionResult New()
{
//re-susing views for add and edit
return View("Form", new CarsForm
{
IsNew = true,
});
}
public ActionResult Edit(int id)
{
var car = Database.Session.Load<Car>(id);
if (car == null)
return HttpNotFound();
return View("Form", new CarsForm
{
...do stuff here to populate form fields!...
});
}
视图模型;
public class CarsForm
{
//do I need the below line?
public IList<CarManufacturer> Manufacturers { get; set; }
public bool IsNew { get; set; }//whether add or edit action to be used
public int? CarId { get; set; }
...more fields
}
非常感谢耐心和帮助新的MVC'er!
啊,现在我看看viewbag方法是如何工作的,我认为我更喜欢将模型传递到DDL,这是我遇到的问题! :) 谢谢! –