1

当我为下拉列表实现代码时,我不断收到此错误。这是我的代码:

String sort = "";
String day_hire = "Daily hire rate";
String man_date = "Manufacturing date";
string man_n = "Manufacturer's name";

if (sl2.SelectedValue.ToString()== man_n)
{
    sort = "man.manufacturer_name";

}
else if (sl2.SelectedValue.ToString() == man_date)
{
    sort = "veh.manufacturing_date";
}
else if (sl2.SelectedValue.ToString() == day_hire) 
{
    sort = "veh.daily_hire_rate";
}
else
{
    sort = "veh.daily_hire_rate"; 
}

这是在构造函数中。鉴于我有:

<%=Html.DropDownList ("sort",((SelectList)ViewData["selectOptions2"]))%>

这是一个运行时错误,显示sl2.SelectedValue.ToString()为空。请帮忙

4

1 回答 1

0

当您执行 POST 时,所选值将存储在您在此处声明的“排序”值中:

<%=Html.DropDownList ("sort",((SelectList)ViewData["selectOptions2"]))%>

所以你需要这样做:

[HttpPost]
public void Whatever()
{
    var selectedItem = Request.Form["sort"] // Or however you want to pick up the value.
}

这是一篇非常好的文章,它更详细地介绍了。

于 2012-09-19T09:21:02.007 回答