[英]Bind DropDownListFor with List on ViewModel
I'm trying to do this:我正在尝试这样做:
This is my ViewModel and Model:这是我的 ViewModel 和模型:
public class OpeningYearViewModel
{
public int OpeningYearId { get; set; }
public string Description { get; set; }
public List<Grade> GradesList { get; set; }
}
public class Grade
{
public int GradeId { get; set; }
public string Name { get; set; }
public int CurrencyId { get; set; }
public int Cost { get; set; }
}
This is my Controller.这是我的控制器。 I build a
SelecList
here and pass it to the view through the ViewBag
我建立一个
SelecList
这里并把它传递到视图通过ViewBag
OpeningYearViewModel viewmodel = new OpeningYearViewModel {
OpeningYearId = 1,
Description = "2015 - II",
GradesList = new List<Grade>
{
new Grade { GradeId = 1, Name = "Grade 1", CurrencyId = 1, Cost = 100 },
new Grade { GradeId = 2, Name = "Grade 2", CurrencyId = 2, Cost = 200 },
new Grade { GradeId = 3, Name = "Grade 3", CurrencyId = 2, Cost = 150 }
}
};
SelectList list = new SelectList(
new List<SelectListItem>
{
new SelectListItem { Text = "S/.", Value = "1"},
new SelectListItem { Text = "$", Value = "2"},
}, "Value" , "Text");
ViewBag.currencyList = list;
return View(viewmodel);
And in my View I need a DropDownListFor for every item on GradesList so I do this:在我的视图中,我需要为 GradesList 上的每个项目一个 DropDownListFor 所以我这样做:
@model Test.Models.OpeningYearViewModel
@for(int i = 0; i < Model.GradesList.Count; i++)
{
@Html.DropDownListFor(x => x.GradesList[i].CurrencyId, new SelectList(ViewBag.currencyList, "Value", "Text"))
@Model.GradesList[i].CurrencyId //This is just to know the CurrencyId on every item.
}
I'm getting every select correctly rendered, but I can't get the correct option selected on the page load: render of view我正在正确呈现每个选择,但我无法在页面加载时选择正确的选项:视图呈现
It is possible to do what I'm trying to do and I'm doing something wrong, or DropDownListFor works in a different way?有可能做我想做的事情但我做错了什么,或者 DropDownListFor 以不同的方式工作?
Thanks!谢谢!
I can't understand why this is happening but you can workaround it by setting explicitly the selected value.我不明白为什么会发生这种情况,但您可以通过明确设置所选值来解决它。 This can be done by passing
Model.GradesList[i].CurrencyId
as fourth parameter to the SelectList
's constructor:这可以通过将
Model.GradesList[i].CurrencyId
作为第四个参数传递给SelectList
的构造函数来完成:
@for(int i = 0; i < Model.GradesList.Count; i++)
{
@Html.DropDownListFor(x => x.GradesList[i].CurrencyId,
new SelectList(ViewBag.currencyList, "Value", "Text", Model.GradesList[i].CurrencyId))
}
Since, the DropDownListFor is used in loop to generate indexed inputs;因为, DropDownListFor 在循环中用于生成索引输入; so generate input controls will be generated with name as "GradeList[0].CurrencyId", "GradeList[1].CurrencyId"......Due to this framework will not bind the selected value in Select List as it is unable to get the value in reflection for selection.
因此生成输入控件将生成名称为“GradeList[0].CurrencyId”、“GradeList[1].CurrencyId”......由于此框架不会绑定选择列表中的选定值,因为它无法获取反射值以供选择。 That's why you have to use the following SelectList constructor to set the selected value.
这就是您必须使用以下 SelectList 构造函数来设置选定值的原因。
public SelectList(
IEnumerable items,
string dataValueField,
string dataTextField,
object selectedValue
)
However, if your DropDownListFor is bind to a simple expression like但是,如果您的 DropDownListFor 绑定到一个简单的表达式,如
@Html.DropDownListFor(model => model.CurrencyId, new SelectList(ViewBag.Items, "Value", "Text"))
then selection will work automatically.然后选择将自动工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.