2014-05-24 17 views
3

我想创建一个表格,将包含一系列下拉列表,所有这些都从数据库中加载。我不知道需要多少下拉列表,或者每个下拉列表在编译时会有多少个选项。我如何建模绑定列表'<SelectItem>'使用MVC.Net

这些字段如何设置以允许它们在发布时建模?

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


型号:

public class MyPageViewModel 
{ 
    public List<MyDropDownListModel> ListOfDropDownLists { get; set; } 
} 

public class MyDropDownListModel 
{ 
    public string Key { get; set; } 
    public string Value { get; set; } 
    public List<SelectListItem> Options { get; set; } 
} 

的控制器获取操作:

[AcceptVerbs(HttpVerbs.Get)] 
[ActionName("MyAction")] 
public ActionResult MyGetAction() 
{ 
    var values_1 = new List<string> {"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<string> {"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> 
       { 
        myDropDownListModel_1, 
        myDropDownListModel_2, 
       } 
     }; 

    return View(model); 
} 

的控制器POST操作:

[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) 
    } 
    <button type="submit">Submit</button> 
} 

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


问题被证明是在视图中在foreach环。将其更改为for-loop会导致帖子按预期填充。更新的观点是如下:

@using (Html.BeginForm("MyPostAction")) 
{ 
    for (int i = 0; i < Model.ListOfDropDownLists.Count; i++) 
{ 
     @Html.HiddenFor(x => x.ListOfDropDownLists[i].Key) 
     @Html.DropDownListFor(m => m.ListOfDropDownLists[i].Value, Model.ListOfDropDownLists[i].Options); 
    } 
    <button type="submit">Submit</button> 
} 
+0

你有什么错误吗? –

+0

@ user256103不会发生错误。当我在post操作中的断点处调试时,模型属性“MyPageViewModel.ListOfDropDownLists”为null。 – Gifreakius

回答

2

您的视图只创建一个名为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操作方法预计类型MyModel)。我认为这些只是错字。

+0

这是对的钱。我还加了一个隐藏字段,以便将帖子返回给Key。 – Gifreakius

0

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

方法基本控制器

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

守则控制器:

DataTable dtList = new DataTable(); 

    dtList = location.GetDistrict(); 
    Dictionary<int, string> 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”)变化(函数(){ VAR TalukaList = “选择” $('#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 + "<option value='" + optionData.Key + "'>" + optionData.Value + "</option>"; 
       }); 
      }, 
      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<int, string> DictionaryTaluka = GenerateDictionaryForDropDown(dtTaluka, "ID", "TalukaName"); 
    return Json(DictionaryTaluka.ToList(), JsonRequestBehavior.AllowGet); 
} 
+0

我不太明白这是否适合这个问题。视图代码将导致只有一个下拉菜单,是否正确?我遇到的模型绑定问题发生在同一个表单中发布多个下拉列表时。 – Gifreakius

+0

你想级联drowdowns? –