简体   繁体   English

来自 SelectList 的 ASP.NET MVC 下拉列表

[英]ASP.NET MVC Dropdown List From SelectList

I am building the following SelectList in my controller.我正在我的控制器中构建以下SelectList

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)

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:看起来我在应该是一个非常简单的下拉列表中为其提供了一组有效的SelectListItem ,但是我没有获得具有良好值和文本的有效<option>列表,而是得到了这个:

<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.据我所知,这应该有效。

You are missing setting the Text and Value field in the SelectList itself.您缺少在 SelectList 本身中设置TextValue字段。 That is why it does a .ToString() on each object in the list.这就是为什么它对列表中的每个对象执行.ToString()的原因。 You could think that given it is a list of SelectListItem it should be smart enough to detect this... but it is not.您可能会认为,鉴于它是一个SelectListItem列表,它应该足够聪明地检测到这一点……但事实并非如此。

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.顺便说一句,您可以使用任何类型的列表或数组...然后只需设置将充当文本和值的属性的名称。

I think it is better to do it like this:我认为最好这样做:

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");

I removed the -1 item, and the setting of each item selected true/false.我删除了-1项,每个项的设置都选择了真/假。

Then, in your view:那么,在你看来:

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

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 中没有将一项设置为选中,则UserType将为 null( UserType需要为int? )。

If you need to set one of the SelectList items as selected, you can use:如果您需要将 SelectList 项目之一设置为选中,您可以使用:

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

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.正如评论中解释的用户之一:当使用DropDownListFor()绑定到属性时,SelectList 构造函数的第四个选项被忽略 - 它是属性的值,决定了选择的内容。

Just try this in razor用剃须刀试试这个

@{
    var selectList = new SelectList(
        new List<SelectListItem>
        {
            new SelectListItem {Text = "Google", Value = "Google"},
            new SelectListItem {Text = "Other", Value = "Other"},
        }, "Value", "Text");
}

and then接着

@Html.DropDownListFor(m => m.YourFieldName, selectList, "Default label", new { @class = "css-class" })

or或者

@Html.DropDownList("ddlDropDownList", selectList, "Default label", new { @class = "css-class" })

Try this, just an example:试试这个,只是一个例子:

u.UserTypeOptions = new SelectList(new[]
    {
        new { ID="1", Name="name1" },
        new { ID="2", Name="name2" },
        new { ID="3", Name="name3" },
    }, "ID", "Name", 1);

Or或者

u.UserTypeOptions = new SelectList(new List<SelectListItem>
    {
        new SelectListItem { Selected = true, Text = string.Empty, Value = "-1"},
        new SelectListItem { Selected = false, Text = "Homeowner", Value = "2"},
        new SelectListItem { Selected = false, Text = "Contractor", Value = "3"},
    },"Value","Text");
var selectList =   new List<SelectListItem>();
selectList.Add(new SelectListItem {Text = "Google", Value = "Google"}) ;
selectList.Add(new SelectListItem {Text = "Other", Value = "Other"}) ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM