ASP.NET MVC使用DropDownLists

时间:2014-01-22 22:08:31

标签: asp.net-mvc forms

我正在使用ASP.NET MVC构建表单,这需要从数据库填充下拉列表。

我设法显示下拉列表,但无论我尝试获取发布数据,应用程序都会抛出各种错误。

在我的模型中:

public IEnumerable<SelectListItem> ApplicationTypeList { get; set; }

我的控制器获取:

public ActionResult Create()
{

    IEnumerable<SelectListItem> ApplicationTypeItems = Lists.ApplicationTypeList.Select(c => new SelectListItem { Value = c.Code, Text = c.Description });
    ViewBag.AppTypes = ApplicationTypeItems;

    return View();
}

其中c.Code是返回表单时我想要的值(字符串),c.Description就是显示的内容。

HttpPost控制器(从脚手架自动生成)

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="PaymentID,ApplicationNumber,ApplicantName,ContactEmail,ContactAddress,ContactPhone")] Payment payment)
        {

            if (ModelState.IsValid)
            {
                db.Payments.Add(payment);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View();
        }

无论出于何种原因,ApplicationTypeList

中都没有[Bind(Include=...

我还必须手动将其添加到Create.cshtml视图中:

<div class="form-group">
            @Html.LabelFor(model => model.ApplicationTypeList, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("ApplicationTypeList", (IEnumerable<SelectListItem>)ViewBag.AppTypes, "Please Select");
                @Html.ValidationMessageFor(model => model.ApplicationTypeList)
            </div>
        </div>

当我提交表单时,我在视图上收到以下错误:

  

没有类型为'IEnumerable'的ViewData项   键有'ApplicationTypeList'。

我浏览过并尝试了其他一些生成列表的方法,例如包含新的String APplicationTypeCode并使用@Html.DropDownListFor(model => model.ApplicationTypeCode, Model.ApplicationTypeList);创建列表但仍会出错。

使用包含下拉列表并将所选值返回给应用程序的表单的最佳方法是什么?

感谢。

1 个答案:

答案 0 :(得分:2)

我建议创建viewmodel

public class InsertPaymentViewModel {
 public SelectList ApplicationTypeList {get; set;}
 // property for selected item
 [Display(Name = "Display name")]
 [Required]
 public int ApplicationTypeSelectedCode {get; set;}
}

在Get动作中使用此模型(我不喜欢复杂形式的ViewBag)

public ActionResult Create()
{
 var model = new InsertPaymentViewModel();
 model.ApplicationTypeItems = new SelectList(Lists.ApplicationTypeList, "Code", "Description ")
 return View(model);
}

查看:

@model PathTo.ViewModels.InsertPaymentViewModel

<div class="form-group">
            @Html.LabelFor(model => model.ApplicationTypeSelectedCode , new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.ApplicationTypeSelectedCode, model.ApplicationTypeItems , "Please Select");
                @Html.ValidationMessageFor(model => model.ApplicationTypeSelectedCode)
            </div>
        </div>

HttpPost行动

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(InsertPaymentViewModel model)
        {

            if (ModelState.IsValid)
            {
                var payment = new Payment 
                { 
                 code = model.InsertPaymentViewModel; //or find reference by code...
                 // ... another property etc.
                }
                db.Payments.Add(payment);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View();
        }