C# 如何使用MVC.Net对“List”列表进行建模绑定
我试图创建一个表单,它将由一系列下拉列表组成,所有这些列表都是从数据库加载的。我不知道需要多少下拉列表,或者每个下拉列表在编译时有多少选项 如何设置这些字段以允许它们在发布时进行模型绑定 下面的每一个代码元素都有很多其他的复杂性,但我无法让模型绑定工作,即使降低到基本级别 模型:C# 如何使用MVC.Net对“List”列表进行建模绑定,c#,asp.net-mvc,model-binding,C#,Asp.net Mvc,Model Binding,我试图创建一个表单,它将由一系列下拉列表组成,所有这些列表都是从数据库加载的。我不知道需要多少下拉列表,或者每个下拉列表在编译时有多少选项 如何设置这些字段以允许它们在发布时进行模型绑定 下面的每一个代码元素都有很多其他的复杂性,但我无法让模型绑定工作,即使降低到基本级别 模型: public class MyPageViewModel { public List<MyDropDownListModel> ListOfDropDownLists { get; set; } }
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; }
}
观点:
@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循环会导致post按预期填充。更新后的视图如下:
@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>
}
我可以给你我的解决方案,它正在发挥作用: 基本控制器中的方法
//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));
}
从此级联中绑定其他下拉列表:
其他下拉列表:
@Html.DropDownListFor(model => model.TalukaId, new SelectList(Model.TalukaList, "Key", "Value"), new { id = "ddlTaluka", @class = "form-control" })
JQuery代码:
$ddlDist.changefunction{
var TalukaList=选择
$'ddlTaluka'.htmlTalukaList
$.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);
}
您的视图仅创建多个名为dll.Value的select元素和与您的模型无关的重复ID。您需要的是创建名为ListOfDropDownList[0]的元素。Value、ListOfDropDownList[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类型。我假设这些只是打字错误。我不太明白这是否符合问题。查看代码只会导致一个下拉列表,对吗?当多个下拉列表在同一表单中发布。您有任何错误吗?@user256103没有错误发生。当我在post操作的断点处调试时,模型属性“MyPageViewModel.ListofDropDownList”为空。这是正确的。我还添加了一个隐藏字段,以在发布时返回密钥。
@Html.DropDownListFor(model => model.TalukaId, new SelectList(Model.TalukaList, "Key", "Value"), new { id = "ddlTaluka", @class = "form-control" })
$.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);
});
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);
}
for (int i = 0; i < Model.ListOfDropDownLists.Count; i++)
{
@Html.DropDownListFor(m => m.ListOfDropDownLists[i].Value, Model.ListOfDropDownLists[i].Options);
}