Asp.net mvc 4 MVC网格表下拉选择值
如何设置“选定值”下拉列表?以下选项不设置任何选定值Asp.net mvc 4 MVC网格表下拉选择值,asp.net-mvc-4,razor,Asp.net Mvc 4,Razor,如何设置“选定值”下拉列表?以下选项不设置任何选定值 @for (int i=0; i< Model.Students.Count;i++) { <tr> <td>@Html.TextBoxFor(x => Model.Students[i].Name, new { @class = "form-control" })
@for (int i=0; i< Model.Students.Count;i++)
{
<tr>
<td>@Html.TextBoxFor(x => Model.Students[i].Name, new { @class = "form-control" })
@Html.ValidationMessageFor(x => Model.Students[i].Name)
</td>
<td>@Html.TextBoxFor(x => Model.Students[i].Age, new { @class = "form-control" })</td>
<td>
@*@Html.DropDownList("country", Model.Countries,)*@
@Html.DropDownListFor(x => Model.Students[i].Country, Model.Countries)
</td>
</tr>
}
@for(int i=0;iModel.Students[i].Name,新{@class=“form control”})
@Html.ValidationMessageFor(x=>Model.Students[i].Name)
@Html.TextBoxFor(x=>Model.Students[i].Age,new{@class=“form control”})
@*@Html.DropDownList(“国家”,Model.Countries,)*@
@DropDownListFor(x=>Model.Students[i].Country,Model.Countries)
}
行动结果:
public ActionResult ShowServerSideGrid()
{
var list = new List<SelectListItem>
{
new SelectListItem { Value = "1", Text = "USA" },
new SelectListItem { Value = "2", Text = "UK" },
new SelectListItem { Value = "3", Text = "China" },
new SelectListItem { Value = "4", Text = "Russia" }
};
var students = new List<StudentModel>();
students.Add(new StudentModel { Country = 1,Age = 24,Name = "Arjun"});
students.Add(new StudentModel { Country = 2, Age = 24, Name = "Kumar" });
students.Add(new StudentModel { Country = 3, Age = 45, Name = "Shiva" });
students.Add(new StudentModel { Country = 4, Age = 60, Name = "Aryal" });
StudentListModel listModel=new StudentListModel();
listModel.Students = students;
listModel.Countries = list;
return View(listModel);
}
public ActionResult ShowServerSideGrid()
{
变量列表=新列表
{
新建SelectListItem{Value=“1”,Text=“USA”},
新建SelectListItem{Value=“2”,Text=“UK”},
新建SelectListItem{Value=“3”,Text=“China”},
新建SelectListItem{Value=“4”,Text=“Russia”}
};
var students=新列表();
Add(新的StudentModel{Country=1,Age=24,Name=“Arjun”});
添加(新的StudentModel{Country=2,Age=24,Name=“Kumar”});
添加(新的StudentModel{Country=3,Age=45,Name=“Shiva”});
添加(新的StudentModel{Country=4,Age=60,Name=“Aryal”});
StudentListModel listModel=新的StudentListModel();
listModel.Students=Students;
listModel.Countries=列表;
返回视图(listModel);
}
您需要在模型中使用列出下拉值{get;set;}
用控制器中的值填充dropDownValues字段
例如
。。。。。。。诸如此类
在您看来,您必须使用语法
@Html.DropDownFor(x => Model.Students[i].Country, Model.dropDownValues)
请使用下面的代码来解决它
@Html.DropDownListFor(x => Model.Students[i].Country, new SelectList(Model.Countries,"Value","Text",Model.Students[i].Country))
查看传递给视图的模型。您正在将的下拉列表绑定到Model.Students[i].Country。确保该字段设置为与Model.countries中的值匹配的值。如果检查actionresult,则值Country设置为与SelectListItems中的值匹配,这可能与foreach有关。尝试在foreach外部放置一个下拉列表,查看是否已设置。另外,请尝试设置其中一个选择列表项的selected属性,以查看下拉列表是否与我在循环外部尝试但未设置的下拉列表有所不同:@Html.DropDownListFor(x=>Model.Students[2].Country,Model.Countries)尝试设置列表中的selected属性在循环中创建SelectList时应提供SelectedValue。所以这应该被标记为正确答案。
@Html.DropDownListFor(x => Model.Students[i].Country, new SelectList(Model.Countries,"Value","Text",Model.Students[i].Country))