0
我有一個下拉,一個文本框和一個按鈕,DropDdown包含COLUMNNAMES(名稱,CPR),看到濾波的數據用戶從下拉菜單中的值時,進入串並點擊按鈕。爲了做到這一點,我寫了下面的代碼,但無法得到DropDown的選定值,除了這個問題代碼working.plz告訴我在哪裏做錯誤。 以下是我的控制器 如何使用下拉列表選擇的值來過濾數據
public ActionResult Index(string searchType,FormCollection frm)
{
List<SelectListItem> obj = new List<SelectListItem>();
obj.Add(new SelectListItem { Text = "Agent Name", Value = "Name", Selected = true });
obj.Add(new SelectListItem {Text = "CPR Number", Value = "Cpr"});
ViewBag.cmb = obj;
var agents = from s in db.Agents
select s;
if (searchType != null)
{
ViewBag.searchString = searchType;
string cmbColumnSelection = frm["cmb"];
switch (cmbColumnSelection)
{
case "Name": // Table=Agent,Column=Name
agents = agents.Where(s => s.Name.ToString().Contains(searchType));
break;
default:
agents = agents.Where(s => s.Cpr.ToString().Contains(searchType));
break;
}
}
return View(agents.ToList());
} `
在View:
@using (Html.BeginForm("Index", "Agent", FormMethod.Get))
{
<p>
@Html.TextBox("searchType", ViewBag.searchString as string)
Select Filter : @Html.DropDownList("cmb")
<input type="submit" value="Submit" class="btn btn-primary" />
</p>
}
不要使用形式收集,只是聲明的動作參數:'指數(字符串檢索類別,字符串CMB)' – Andrei
那麼我怎麼能在這裏獲取DropDownList的值? 'string cmbColumnSelection = frm [「cmb」];' – tahirraza
它在輸入! 'String cmb'應該包含你需要的值 – Andrei