简体   繁体   English

将 DropDownListFor 与 ViewModel 上的列表绑定

[英]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.

相关问题 使用除dataValueField / dataTextField之外的第三个值将ViewModel绑定到DropDownListFor - Bind a ViewModel to a DropDownListFor with a third value besides dataValueField/dataTextField 无法将多个生成的Html.DropDownListFor绑定到ViewModel - Unable to Bind Multiple Generated Html.DropDownListFor to ViewModel 将视图模型中的列表与字典绑定到复选框 - Bind Dictionary with list in viewmodel to checkboxes DropDownListFor ViewModel中的Collection属性 - DropDownListFor Properties of Collection in ViewModel 如何将ViewModel与DropDownListFor一起使用 - How to use ViewModel with DropDownListFor 使用viewmodel在asp.net-MVC中使用html.dropdownlistFor绑定数据 - Use viewmodel to bind data with html.dropdownlistFor in asp.net-MVC 子模型列表上下文中的DropDownListFor不绑定值 - DropDownListFor in the context of a list of child models doesn't bind value 如何从 appsettings.json 读取列表并将其绑定到 mvc dropdownlistfor() - How to read a list from appsettings.json and bind it in the mvc dropdownlistfor() 将ViewModel与列表绑定 <T> 到特定的控制 - Bind ViewModel with List<T> to a specific control 将 ASP.NET ViewModel 绑定到列表列表 - Bind ASP.NET ViewModel to List of Lists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM