ASP.NET MVC Html.DropDownList的SelectedValue [英] ASP.NET MVC Html.DropDownList SelectedValue

查看:86
本文介绍了ASP.NET MVC Html.DropDownList的SelectedValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经试过这是RC1,然后升级到RC2这并不能解决问题。

I have tried this is RC1 and then upgraded to RC2 which did not resolve the issue.

// in my controller
ViewData["UserId"] = new SelectList(
    users, 
    "UserId", 
    "DisplayName", 
    selectedUserId.Value); // this has a value

结果:SelectedValue属性被设置在对象上

result: the SelectedValue property is set on the object

// in my view
<%=Html.DropDownList("UserId", (SelectList)ViewData["UserId"])%>

结果:所有预期选项呈现给客户端,但所选择的属性未设置​​。在该的SelectedValue项目存在在列表中,但在列表中的第一项总是默认为选中。

result: all expected options are rendered to the client, but the selected attribute is not set. The item in SelectedValue exists within the list, but the first item in the list is always defaulted to selected.

应该怎么做呢?

更新
感谢John Feminella的答复我发现了问题是什么。 用户ID是模型我的看法是强类型的一个属性。当Html.DropDownList(用户ID更改为任何其他名称,但用户ID,所选择的值正确呈现。

Update Thanks to John Feminella's reply I found out what the issue is. "UserId" is a property in the Model my View is strongly typed to. When Html.DropDownList("UserId" is changed to any other name but "UserId", the selected value is rendered correctly.

这会导致无法绑定到模型的价值虽然。

This results in the value not being bound to the model though.

推荐答案

这是我如何解决了这个问题:

This is how I fixed this problem:

我有以下内容:

控制器:

ViewData["DealerTypes"] = Helper.SetSelectedValue(listOfValues, selectedValue) ;

查看

<%=Html.DropDownList("DealerTypes", ViewData["DealerTypes"] as SelectList)%>

通过以下更改:

查看

<%=Html.DropDownList("DealerTypesDD", ViewData["DealerTypes"] as SelectList)%>

看来,下拉不能有相同名称的ViewData的名称:S奇怪,但它的工作。

It appears that the DropDown must not have the same name has the ViewData name :S weird but it worked.

这篇关于ASP.NET MVC Html.DropDownList的SelectedValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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