如何获取DropDownList控件中选中的值
IQuery.aspx中,代码如下:
<p>
<label for="labname">按名字查找:</label>
<%=Html.DropDownList("DropDownListName")%>
<input type="submit" value="查询" />
</p>
<table>
<tr>
<th>
Name
</th>
<th>
Sex
</th>
<th>
BirthDate
</th>
<th>
MobilePhone
</th>
<th>
EMail
</th>
</tr>
<%foreach (var item in Model) { %>
<tr>
<td>
<%: item.Name %>
</td>
<td>
<%: item.Sex %>
</td>
<td>
<%: String.Format("{0:g}", item.BirthDate) %>
</td>
<td>
<%: item.MobilePhone %>
</td>
<td>
<%: item.EMail %>
</td>
</tr>
<% } %>
AddressBookRepository addressbookrepository = new AddressBookRepository();
public ActionResult IQuery(FormCollection formcollection)
{
string name = formcollection["DropDownListName"].ToString();
ViewData["DropDownListName"] = new SelectList(db.tbAddressBooks, "Name", "Name");
var addressbooks = addressbookrepository.GetAddressBookInName(name).ToList();//GetAddressBookInName()是一个函数,根据名字查询记录
return View("IQuery", addressbooks);
}
断点调试时name的值为空,不知道什么原因,请大家帮帮忙,刚开始学,比较菜~~~
[解决办法]
那你查下formcollection啊,估计他里面本来就没有key为DropDownListName的value啊
[解决办法]
直接不可以用DropDownListName.selectValue么.
[解决办法]
Request.From["DropDownListName"]
[解决办法]
DropDownListName.Text(如果是文本的话),DropDownListName.selectedValue