Html.DropDownList绑定整数

时间:2011-03-26 08:07:33

标签: asp.net-mvc

我的控制器中有以下代码:

Values = new SelectList(new[] {10, 25, 50});

在我看来,我有:

 <%= Html.DropDownList("selItemsPerPage1", Model.Items.Values,
new {
 onchange="something", title="something"
});

在结果输出中显示列表 System.Web.Mvc.SelectList.

如何让它显示整数呢?

1 个答案:

答案 0 :(得分:3)

在控制器中:

Values = new SelectList(
    new[] 
    {
        new SelectListItem { Value = "10", Text = "10" },
        new SelectListItem { Value = "25", Text = "25" },
        new SelectListItem { Value = "50", Text = "50" },
    },
    "Value", "Text"
);

或者如果您愿意:

var values = new[] { 10, 25, 50 }.Select(x => new SelectListItem
{
    Value = x.ToString(),
    Text = x.ToString()
});
Values = new SelectList(values, "Value", "Text");

并在视图中:

<%= Html.DropDownList(
    "selItemsPerPage1", 
    Model.Items.Values,
    new {
        onchange = "something", 
        title = "something"
    }
) %>