[英]selectList.SelectedValue is null
I keep getting this error when I implement my code for a dropdown list. 当我为下拉列表实现代码时,我总是收到此错误。 Here is my code:
这是我的代码:
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";
}
this is in constructor. 这是在构造函数中。 In view I've got:
鉴于我有:
<%=Html.DropDownList ("sort",((SelectList)ViewData["selectOptions2"]))%>
it's a runtime error that says sl2.SelectedValue.ToString()
is null. 这是一个运行时错误,
sl2.SelectedValue.ToString()
为null。 Please help 请帮忙
When you do a POST, the selected value will be stored in the "sort" value, which you had declared here: 当您执行POST时,所选值将存储在您在此处声明的“ sort”值中:
<%=Html.DropDownList ("sort",((SelectList)ViewData["selectOptions2"]))%>
So you need to do: 因此,您需要执行以下操作:
[HttpPost]
public void Whatever()
{
var selectedItem = Request.Form["sort"] // Or however you want to pick up the value.
}
This is a very good article which goes into more detail. 这是一篇非常不错的文章 ,将进行更详细的介绍。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.