MVC 绑定模型到视图中的下拉列表

MVC binding model to dropdownlist in view

我是 MVC 的新手,正在尝试使用 WebForms 摆脱形式,但有些事情让我感到困惑,不知道我需要什么才能完成某些事情。

在我看来“/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 用户的耐心和帮助!

这是一种方法,也是我个人在小型项目中的首选方法,但是有几种方法可以给这只猫蒙皮。

希望您会注意到 Html.DropDownListFor 需要一个 List<SelectListItem> 这样您就可以继续在您的视图模型中声明一个,但是您还需要 Id选择的特定制造商:

public class CarsForm
{
    public int ManufacturerId { get; set; }
    public List<SelectListItem> Manufacturers { get; set; }

    // Other properties
}

附带说明一下,例如,视图模型的命名约定为 CreateCarViewModel

在您的 New 方法中,您需要从数据库实例化 Manufacturers,通常是这样的:

viewModel.Manufacturers = (from m in dbContext.Manufacturers
                          orderby m.Name
                          select new SelectLisItem { Text = m.Name, Value = m.Id.ToString() }).ToList()

现在,在您的 View 中,您可以:

@Html.DropDownListFor(model => model.Manufacturers, Model.ManufacturerId, "Please select")

我通常将下拉列表数据添加到viewbag。您可以从视图模型和实体中删除列表 class。

你的控制器看起来像这样。

public ActionResult New()
{
    ViewBag.Manufacturers = new SelectList(
        Database.Session.Load<Manufacturer>(), 
        "Manufacturer", 
        "Manufacturer");
    return View("Form", new CarsForm
    {
        IsNew = true,
    });
}

public ActionResult Edit(int id)
{
    var car = Database.Session.Load<Car>(id);
    if (car == null)
        return HttpNotFound();

    ViewBag.Manufacturers = new SelectList(
        Database.Session.Load<Manufacturer>(), 
        "Manufacturer", 
        "Manufacturer", 
        car.Manufacturer); //sets the initial value
    return View("Form", new CarsForm
    {
        ...do stuff here to populate form fields!...
    });
}

然后在您看来,您只需添加 @Html.DropDownListFor

@Html.DropDownListFor(model => model.Manufacturer, (SelectList)ViewBag.Manufacturers)