使用DropDownLists的ASP.NET MVC

杰克

我正在使用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”。

我环顾四周,并尝试了其他几种生成列表的方法,例如包括使用newString APplicationTypeCode并创建列表,@Html.DropDownListFor(model => model.ApplicationTypeCode, Model.ApplicationTypeList);但仍然会出错。

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

谢谢。

手动的

我建议创建视图模型

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

在“采取行动”中使用此模型(我不喜欢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();
        }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章