如何从mvc3中的下拉列表中获取值 [英] How To get value from drop down list in mvc3
本文介绍了如何从mvc3中的下拉列表中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
i我想从下拉列表中选择一个值
1st have have使用FormCollection从表单中获取所有输入值,从现在我想要取得所选下拉列表的值
直到我完成
我的cshtml是我的复选框和下拉列表
< td > < 输入 type = 复选框 name = 'chk#'@item.CountryDetails.CountryId id = 'chk#'@item.CountryDetails.CountryId / > < / td >
< td > < 选择 id = ' DRP#'@ item.CountryDetails.CountryId name = 'DRP#'@item.CountryDetails.C ountryId > < option value = null > - 选择时间 - < / option >
< 选项 value = 每月 > 每月< / option >
< span class =code-keyword>< 选项 value = year > 每年< / option >
控制器我写的是
列表schObj = new List(); // 这是在模型中创建类
调度WSH = new Scheduling();
for ( int i = 0 ; i < form.Count; i ++)
{
if (form [i] == on)
{
WSH.Scheduled = true ;
string CID = form.AllKeys [i];
string [] splitnum = null ;
splitnum = CID.Split(' #');
CID = splitnum [ 1 ]。ToString();
CID = CID.Replace( ', );
WSH.CountryId = Convert.ToInt64(CID);
schObj.Add(WSH);
}
现在我想从下拉列表中获取所选值
解决方案
您可以使用formcollection从下拉列表中获取选定的值
示例:
public ActionResult Create(FormCollection collection)
{
try
{
int Country = Convert.ToInt32(collection [Country]);
int State = Convert.ToInt32(collection [State]);
int City = Convert.ToInt32(collection [City]);
返回RedirectToAction(Index);
}
catch
{
return View();
}
}
凡是集合[国家],国家/地区是下拉列表控制ID等
Hi Everyone
i am trying to get a selected value from drop down list
1st have taken all inputs value from form using FormCollection in that from the from id now i want take the value of the selected dropdown list
till i have done
my cshtml is i have check box and dropdown list
<td><input type="checkbox" name="'chk#'@item.CountryDetails.CountryId" id="'chk#'@item.CountryDetails.CountryId" /></td>
<td><select id="'DRP#'@item.CountryDetails.CountryId" name="'DRP#'@item.CountryDetails.CountryId"><option value="null">--Select time--</option>
<option value="monthly">Monthly</option>
<option value="yearly">Yearly</option>
in controller i ma writing
List schObj = new List();// this is the created Class in model
Scheduling WSH = new Scheduling();
for (int i = 0; i < form.Count; i++)
{
if (form[i] == "on")
{
WSH.Scheduled = true;
string CID = form.AllKeys[i];
string[] splitnum = null;
splitnum = CID.Split('#');
CID = splitnum[1].ToString();
CID = CID.Replace("'", "");
WSH.CountryId = Convert.ToInt64(CID);
schObj.Add(WSH);
}
now i want get the selected value from dropdown list
解决方案
You can get selected value from dropdownlist using formcollection
Example:
public ActionResult Create(FormCollection collection) { try { int Country = Convert.ToInt32(collection["Country"]); int State = Convert.ToInt32(collection["State"]); int City = Convert.ToInt32(collection["City"]); return RedirectToAction("Index"); } catch { return View(); } }
Where collection["Country"], Country is dropdown Control ID etc
这篇关于如何从mvc3中的下拉列表中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文