我有一个硬编码下拉列表我希望在点击按钮时在动作方法中获取所选值。
查看
@using (Html.BeginForm("GetSelectedValue","Home"))
{
@Html.DropDownList("Departments", new List<SelectListItem>
{
new SelectListItem { Text = "IT", Value = "1", Selected=true},
new SelectListItem { Text = "HR", Value = "2"},
new SelectListItem { Text = "Payroll", Value = "3"}
}, "Select Department")
<button type="submit">next</button>
}
控制器
public ActionResult GetSelectedValue()
{
string value = drop down value;
//I tried this didnt work
string selvalue = Request["Departments"];
return View();
}
在这种情况下,我不想包含模型或任何视图模型。
答案 0 :(得分:1)
在您的操作方法中添加FormCollection
参数,并获取所选的值,如下所示
public ActionResult GetSelectedValue(FormCollection form)
{
string selectedValue = form["Departments"];
return View();
}
答案 1 :(得分:0)
您可以尝试将其添加为Action的参数,例如:
[HttpPost]
public ActionResult GetSelectedValue(string Departments)
{
// do you stuff here
return View();
}
根据您的RouteConfig,这可能只是开箱即用。