IEnumerable的<串GT;到的SelectList,没有值选择 [英] IEnumerable<string> to SelectList, no value is selected

查看:114
本文介绍了IEnumerable的<串GT;到的SelectList,没有值选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似的东西在ASP.NET MVC应用程序如下:

I have something like the following in an ASP.NET MVC application:

IEnumerable<string> list = GetTheValues();
var selectList = new SelectList(list, "SelectedValue");

甚至想到选定值定义,它没有被在视图中选择。我有这种感觉,我在这里失去了一些东西,所以如果有人可以把我出我的痛苦!

And even thought the selected value is defined, it is not being selected on the view. I have this feeling I'm missing something here, so if anyone can put me out my misery!

我知道我可以使用一个annoymous类型提供键和价值,但我宁愿不添加额外的code。如果我没有到。

I know I can use an annoymous type to supply the key and value, but I would rather not add the additional code if I didn't have to.

编辑:这个问题已经得到修复ASP.NET MVC RTM

This problem has been fixed by ASP.NET MVC RTM.

推荐答案

试试这个:

IDictionary<string,string> list = GetTheValues();
var selectList = new SelectList(list, "Key", "Value", "SelectedValue");

的SelectList(至少在preVIEW 5)是不够聪明地看到IEnumerable的元素是值类型,所以应该使用该项目的价值和文本。相反,它设置每个项目为空或类似的东西的价值。这就是为什么选定的值没有任何影响。

SelectList (at least in Preview 5) is not clever enough to see that elements of IEnumerable are value type and so it should use the item for both value and text. Instead it sets the value of each item to "null" or something like that. That's why the selected value has no effect.

这篇关于IEnumerable的&LT;串GT;到的SelectList,没有值选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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