SelectList的ASP.NET MVC下拉列表

时间:2013-11-27 12:49:16

标签: c# asp.net-mvc drop-down-menu

我正在我的控制器中构建以下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;

并在我的视图中显示如下:

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

看起来我在它应该是一个非常简单的下拉列表中给它一组有效的SelectListItem s,但是我没有得到具有良好值和文本的有效<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>

是什么给出的?据我所知,这应该有效。

3 个答案:

答案 0 :(得分:60)

您缺少在SelectList本身中设置文字这样的字段。这就是它为列表中的每个对象执行.ToString()的原因。您可以认为,鉴于它是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);

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

我认为最好这样做:

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。

然后,在您看来:

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

这样,如果您设置“选择一个”项,并且未在SelectList中设置一个项目,则UserType将为空(UserType必须为{ {1}})。

如果您需要将其中一个SelectList项目设置为选中,则可以使用:

int?

答案 1 :(得分:11)

试试剃须刀

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

然后

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

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

答案 2 :(得分:1)

试试这个,只是一个例子:

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

或者

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