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

时间:2015-07-03 14:58:10

标签: asp.net-mvc drop-down-menu html.dropdownlistfor

我对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));
        }
    }

调用表单的Controller是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的耐心和帮助!

2 个答案:

答案 0 :(得分:0)

我通常会向viewbag添加下拉列表数据。您可以从viewmodel和实体类中删除列表。

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

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)

答案 1 :(得分:-1)

这是一种方法,我个人认为这是一种较小项目的首选方法,但有一些方法可以让这只特别的猫皮肤。

希望您会注意到Html.DropDownListFor需要List<SelectListItem>,因此您可以继续在视图模型中声明一个,但您还需要Id的{​​{1}}特别制造商选择:

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")