ASP.NET MVC下拉列表从的SelectList [英] ASP.NET MVC Dropdown List From SelectList

查看:508
本文介绍了ASP.NET MVC下拉列表从的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 S IN应该是​​什么pretty简单的下拉列表中,但而不是得到一个有效的&LT;选项&gt;在良好的价值观和文本名单,我得到这样的:

It looks like I am giving it a valid set of SelectListItems 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.

推荐答案

您缺少什么设置字段是文字在选择列表本身。这就是为什么它做了的ToString()列表中的每个对象。你可能认为,鉴于这是 SelectListItem 的名单应该是足够聪明来检测这个......但事实并非如此。

You are missing setting what field is the Text and Value 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);

顺便说一句,你可以使用任何类型的数组列表...然后只需设置将作为文本和Value属性的名称。

BTW, you can use a list of 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项目,每个项目的设置中选择真/假。

I removed the -1 item, and the setting of each items selected true/false.

然后,在你的看法:

@Html.DropDownListFor(m => m.UserType, Model.UserTypeOptions, "Select one")

这样,如果你设置了中选择一个项,在选择列表中选择你不设置一个项目,在用户类型将为空(在用户类型必须 INT?)。

如果您需要根据选择设置选择列表项目之一,你可以使用:

If you need to set one of the SelectList items as selected, you can use:

u.UserTypeOptions = new SelectList(options, "Value" , "Text", userIdToBeSelected);

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

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