来自 SelectList 的 ASP.NET MVC 下拉列表 [英] ASP.NET MVC Dropdown List From SelectList
问题描述
我正在我的控制器中构建以下 SelectList
.
I am building the following SelectList
in my controller.
var u = new NewUser();
u.UserTypeOptions = new SelectList(new List<SelectListItem>
{
new SelectListItem { Selected = true, Text = string.Empty, Value = "-1"},
new SelectListItem { Selected = false, Text = "Homeowner", Value = ((int)UserType.Homeowner).ToString()},
new SelectListItem { Selected = false, Text = "Contractor", Value = ((int)UserType.Contractor).ToString()},
});
return u;
并像这样在我的视图中显示它:
And displaying it on my view like this:
@Html.DropDownListFor(m => m.UserType, Model.UserTypeOptions)
看起来我在应该是一个非常简单的下拉列表中为它提供了一组有效的 SelectListItem
,但没有获得有效的 具有良好值和文本的列表,我明白了:
It looks like I am giving it a valid set of SelectListItem
s in what should be a pretty straightforward dropdown list, but instead of getting a valid <option>
list with good values and text, I get this:
<select data-val="true" data-val-range="A user type must be selected." data-val-range-max="2" data-val-range-min="1" data-val-required="The UserType field is required." id="UserType" name="UserType" class="input-validation-error">
<option>System.Web.Mvc.SelectListItem</option>
<option>System.Web.Mvc.SelectListItem</option>
<option>System.Web.Mvc.SelectListItem</option>
</select>
什么给?据我所知,这应该可行.
What gives? As far as I can tell, this should work.
推荐答案
您没有在 SelectList 本身中设置 Text 和 Value 字段.这就是为什么它对列表中的每个对象执行 .ToString()
的原因.您可能会认为,鉴于它是一个 SelectListItem
列表,它应该足够智能以检测到这一点……但事实并非如此.
You are missing setting the Text and Value field in the SelectList itself. That is why it does a .ToString()
on each object in the list. You could think that given it is a list of SelectListItem
it should be smart enough to detect this... but it is not.
u.UserTypeOptions = new SelectList(
new List<SelectListItem>
{
new SelectListItem { Selected = true, Text = string.Empty, Value = "-1"},
new SelectListItem { Selected = false, Text = "Homeowner", Value = ((int)UserType.Homeowner).ToString()},
new SelectListItem { Selected = false, Text = "Contractor", Value = ((int)UserType.Contractor).ToString()},
}, "Value" , "Text", 1);
顺便说一句,您可以使用任何类型的列表或数组...然后只需设置将充当文本和值的属性的名称.
BTW, you can use a list or array of any type... and then just set the name of the properties that will act as Text and Value.
我认为这样做更好:
u.UserTypeOptions = new SelectList(
new List<SelectListItem>
{
new SelectListItem { Text = "Homeowner", Value = ((int)UserType.Homeowner).ToString()},
new SelectListItem { Text = "Contractor", Value = ((int)UserType.Contractor).ToString()},
}, "Value" , "Text");
我去掉了 -1 项,每个项的设置选择了 true/false.
I removed the -1 item, and the setting of each item selected true/false.
那么,在你看来:
@Html.DropDownListFor(m => m.UserType, Model.UserTypeOptions, "Select one")
这样,如果您将选择一个"设置为item 并且不要将 SelectList 中的一项设置为已选中,UserType
将为 null(UserType
需要为 int?
).
This way, if you set the "Select one" item and don't set one item as selected in the SelectList, the UserType
will be null (the UserType
need to be int?
).
如果您需要将 SelectList 项之一设置为选中,您可以使用:
If you need to set one of the SelectList items as selected, you can use:
u.UserTypeOptions = new SelectList(options, "Value" , "Text", userIdToBeSelected);
正如评论中的一位用户所解释的:当使用 DropDownListFor()
绑定到一个属性时,SelectList 构造函数的第 4 个选项被忽略 - 它是决定选择什么的属性的值.
As one of the users explained in the comments:
The 4th option of the SelectList constructor is ignored when binding to a property using DropDownListFor()
- it is the property's value that determines what is selected.
这篇关于来自 SelectList 的 ASP.NET MVC 下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!