如何使用MVC.Net建模绑定“List ”列表

我正在尝试创建一个由一系列下拉列表组成的表单,所有这些列表都是从数据库加载的。 我不知道需要多少下拉列表,或者每个下拉列表在编译时有多少选项。

如何设置这些字段以允许它们在发布时进行模型绑定?

在下面的每个代码元素中都有很多其他的复杂性,但是即使在降低到​​基本级别时,我也无法使模型绑定工作。


型号:

public class MyPageViewModel { public List ListOfDropDownLists { get; set; } } public class MyDropDownListModel { public string Key { get; set; } public string Value { get; set; } public List Options { get; set; } } 

控制器获取操作:

 [AcceptVerbs(HttpVerbs.Get)] [ActionName("MyAction")] public ActionResult MyGetAction() { var values_1 = new List {"Val1", "Val2", "Val3"}; var options_1 = values_1 .ConvertAll(x => new SelectListItem{Text=x,Value=x}); var myDropDownListModel_1 = new MyDropDownListModel { Key = "Key_1", Options = options_1 }; var values_2 = new List {"Val4", "Val5", "Val6"}; var options_2 = values_2 .ConvertAll(x => new SelectListItem{Text=x,Value=x})}; var myDropDownListModel_2 = new MyDropDownListModel { Key = "Key_2", Options = options_2 }; var model = new MyPageViewModel { ListOfDropDownLists = new List { myDropDownListModel_1, myDropDownListModel_2, } }; return View(model); } 

控制器发布动作:

 [AcceptVerbs(HttpVerbs.Post)] [ActionName("MyAction")] public ActionResult MyPostAction(MyPageViewModel model) { //Do something with posted model... //Except 'model.ListOfDropDownLists' is always null return View(model); } 

风景:

 @model MyPageViewModel @using (Html.BeginForm("MyPostAction")) { foreach (var ddl in Model.ListOfDropDownLists) { @Html.DropDownListFor(x => ddl.Value, ddl.Options) }  } 

编辑 :更正了拼写错误和复制粘贴错误。


方案

问题原来是视图中的foreach循环。 将其更改为for循环会导致post按预期填充。 更新后的视图如下:

 @using (Html.BeginForm("MyPostAction")) { for (int i = 0; i  x.ListOfDropDownLists[i].Key) @Html.DropDownListFor(m => m.ListOfDropDownLists[i].Value, Model.ListOfDropDownLists[i].Options); }  } 

您的视图仅创建多个名为dll.Value (和重复ID)的选择元素,这些元素与您的模型无关。 你需要的是创建名为ListOfDropDownLists[0].Value, ListOfDropDownLists[1].Value等的元素。

将视图中的循环更改为此

 for (int i = 0; i < Model.ListOfDropDownLists.Count; i++) { @Html.DropDownListFor(m => m.ListOfDropDownLists[i].Value, Model.ListOfDropDownLists[i].Options); } 

您发布的代码有多个错误(例如,您传递的是MyPageViewModel类型的模型,但post action方法需要MyModel类型)。 我认为这些只是错字。

我可以给你我的解决方案,它的工作原理是:

基本控制器中的方法

 //To bind Dropdown list protected Dictionary GenerateDictionaryForDropDown(DataTable dtSource, string keyColumnName, string valueColumnName) { return dtSource.AsEnumerable() .ToDictionary(row => row.Field(keyColumnName), row => row.Field(valueColumnName)); } 

控制器中的代码:

  DataTable dtList = new DataTable(); dtList = location.GetDistrict(); Dictionary DistrictDictionary = GenerateDictionaryForDropDown(dtList, "Id", "DistrictName"); model.DistrictList = DistrictDictionary; 

在视图中绑定数据:

  @Html.DropDownListFor(model => model.DiscrictId, new SelectList(Model.DistrictList, "Key", "Value"), new { id = "ddlDist", @class = "form-control" }) 

绑定其他下拉列表(级联):其他下拉列表:

 @Html.DropDownListFor(model => model.TalukaId, new SelectList(Model.TalukaList, "Key", "Value"), new { id = "ddlTaluka", @class = "form-control" }) 

JQuery代码:$(“#ddlDist”)。change(function(){var TalukaList =“Select”$(’#ddlTaluka’)。html(TalukaList);

  $.ajax({ type: "Post", dataType: 'json', url: 'GetTaluka', data: { "DistId": $('#ddlDist').val() }, async: false, success: function (data) { $.each(data, function (index, optionData) { TalukaList = TalukaList + ""; }); }, error: function (xhr, status, error) { //alert(error); } }); $('#ddlTaluka').html(TalukaList); }); 

控制器方法返回JSON

 public JsonResult GetTaluka(int DistId) { LocationDH location = new LocationDH(); DataTable dtTaluka = location.GetTaluka(DistId); Dictionary DictionaryTaluka = GenerateDictionaryForDropDown(dtTaluka, "ID", "TalukaName"); return Json(DictionaryTaluka.ToList(), JsonRequestBehavior.AllowGet); }