如何在mvc 4.0中获取Dropdownlist选择的值 [英] How to get Dropdownlist selected value in mvc 4.0
本文介绍了如何在mvc 4.0中获取Dropdownlist选择的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是MVC的新手。我希望控制器上的下拉列表选择值提交按钮Click.How得到这个?下面是我的代码
COntroller: -
public ActionResult BindDropdown()
{
List < SelectListItem > obj = new List < SelectListItem > ();
obj = TU.Getlist();
ViewBag.StateType = obj;
return View();
}
[HttpPost]
public ActionResult Bindgrid()
{
int id = 转换 .ToInt32(请求[ StateType]。ToString ());
int id = Convert.ToInt32(Request.Form [ StateType跨度>]的ToString());
}
查看:: -
用户名:
@ Html.DropDownList(StateType,请选择用户)
@using(Html.BeginForm( Bindgrid,新))
{
< input type =submitvalue =Get Queriesstyle ='width:300px'/>
}
解决方案
你好,
你可以在HttpPost方法中传递FormCollection来获取值。< br />
< pre lang =cs> [HttpPost]< br />
public ActionResult Bindgrid(FormCollection frm)< br />
{< br />
var id = frm [StateType];< br />
< br />
//做其他事情< br />
返回View();< br />
}< br />
< / pre>
Hi,
I am novice to MVC. I want to get dropdownlist selected value on Controller on submit button Click.How to get this?below is my code
COntroller:-
public ActionResult BindDropdown()
{
List<SelectListItem> obj = new List<SelectListItem>();
obj = TU.Getlist();
ViewBag.StateType = obj;
return View();
}
[HttpPost]
public ActionResult Bindgrid()
{
int id = Convert.ToInt32(Request["StateType"].ToString());
int id =Convert.ToInt32( Request.Form["StateType"].ToString());
}
VIEW::-
User Name:
@Html.DropDownList("StateType", "Please select an User")
@using (Html.BeginForm("Bindgrid", "New"))
{
<input type="submit" value="Get Queries" style='width:300px'/>
}
解决方案
Hello ,
You may pass theFormCollection in the HttpPost method to get the value.<br /> <pre lang="cs">[HttpPost]<br /> public ActionResult Bindgrid(FormCollection frm)<br /> {<br /> var id= frm["StateType"];<br /> <br /> //do other stuffs <br /> return View();<br /> }<br /> </pre>
这篇关于如何在mvc 4.0中获取Dropdownlist选择的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文