MVC3 @ Html.DropDownListFor不填充所选项目 [英] MVC3 @Html.DropDownListFor not populating selected item

查看:86
本文介绍了MVC3 @ Html.DropDownListFor不填充所选项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在MVC3应用程序和选择的项目没有显示的值下面的下拉列表。我的下拉列表中包含自定义类的列表。我的看法code是如下:

I have the following dropdown list in an MVC3 application and the selected item is not showing a value. My dropdown list is contained in a list of a custom class. My view code is as follows.

...

for (int i = 0; i < Model.PartAttributes.Count; i++)
{
    if (Model.PartAttributes[i].IsActive)
    {
        <div class="row inline-inputs">
            <label class="span5">@Model.PartAttributes[i].DisplayName:</label>
            @Html.DropDownListFor(m => m.PartAttributes[i].Value, Model.PartAttributes[i].PartList, "Choose Part")
            @Html.TextBoxFor(model => Model.PartAttributes[i].Value, new { @class = "mini" })
            @Html.ValidationMessageFor(model => Model.PartAttributes[i].Value)
            @Html.HiddenFor(model => model.PartAttributes[i].AttributeName)

        </div>
    }
}
...

下拉框下方的文本框中正确填写和列表选项正确填写。而选定的选项是在选项挑列表。我能是做错了?

The text box under the dropdown box fills correctly and the list options fill correctly. And the selected option is in the list of options to pick. What can I be doing wrong?

推荐答案

尝试这样的:

@Html.DropDownListFor(
    m => m.PartAttributes[i].Value, 
    new SelectList(
        Model.PartAttributes[i].PartList, 
        "Value", 
        "Text", 
        Model.PartAttributes[i].Value
    ), 
    "Choose Part"
)

据我所知的 DropDownListFor 助手是无法确定从拉姆达前pression是作为第一个参数传递选择的值,如果这个拉姆达前pression重presents与集合复杂的嵌套属性。用简单的属性,虽然作品: M =&GT; m.FooBar 。我知道这有点很烂,但我希望这将是固定在未来的版本。

AFAIK the DropDownListFor helper is unable to determine the selected value from the lambda expression that is passed as first argument if this lambda expression represents complex nested properties with collections. Works with simple properties though: m => m.FooBar. I know that it kinda sucks, but hopefully this will be fixed in future versions.

这篇关于MVC3 @ Html.DropDownListFor不填充所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆