如何获得的DropDownList的SelectedValue的控制器在MVC [英] How to get DropDownList SelectedValue in Controller in MVC
问题描述
我的DropDownList,我从数据库填补。现在我需要得到控制选定值做一些处理。但是没有得到这个想法。 code,我都试过了。
模型
公共类MobileViewModel
{
公开名单< tbInsertMobile> MobileList;
公众的SelectList供应商{搞定;组; }
}
控制器
公众的ActionResult ShowAllMobileDetails()
{
MobileViewModel MV =新MobileViewModel();
MV.MobileList = db.Usp_InsertUpdateDelete(空,,,空,,4,MergeOption.AppendOnly).ToList();
MV.Vendor =新的SelectList(db.Usp_VendorList(),VENDORID,VENDORNAME);
返回视图(MV);
} [HttpPost]
公众的ActionResult ShowAllMobileDetails(MobileViewModel MV)
{
字符串strDDLValue =; //这里我需要的DropDownList值 返回视图(MV);
}
查看
<表>
&所述; TR>
< TD>移动Manufacured< / TD>
< TD> @ Html.DropDownList(ddlVendor,Model.Vendor,选择Manufacurer)< / TD>
< / TR>
&所述; TR>
&所述; TD> < / TD>
&所述; TD>
<输入ID =Submit1类型=提交值=搜索/>
< / TD>
< / TR>
< /表>
1的方法(通过请求或的FormCollection):
您可以从请求
使用的Request.Form
读它,你的下拉列表名称为 ddlVendor
所以通过 ddlVendor
键在的FormCollection获得由表单提交它的值:
字符串strDDLValue =的Request.Form [ddlVendor]的ToString()。
或使用的FormCollection
:
[HttpPost]
公众的ActionResult ShowAllMobileDetails(MobileViewModel MV,形式的FormCollection)
{
字符串strDDLValue =形式[ddlVendor]的ToString()。 返回视图(MV);
}
第二个方法(通过模型):
如果你想用模型绑定然后模型添加属性:
公共类MobileViewModel
{
公开名单< tbInsertMobile> MobileList;
公众的SelectList供应商{搞定;组; }
公共字符串SelectedVender {获取;设置;}
}
和中查看:
@ Html.DropDownListFor(M = GT; m.SelectedVender,Model.Vendor,选择Manufacurer)
和在行动:
[HttpPost]
公众的ActionResult ShowAllMobileDetails(MobileViewModel MV)
{
字符串的SelectedValue = MV.SelectedVendor;
返回视图(MV);
}
更新:
如果您要发布所选项目的文本,以及,你必须添加一个隐藏字段和下拉菜单中的隐藏字段选择变更集中选择的项目文本:
公共类MobileViewModel
{
公开名单< tbInsertMobile> MobileList;
公众的SelectList供应商{搞定;组; }
公共字符串SelectVendor {获取;设置;}
公共字符串SelectedvendorText {搞定;组; }
}
使用jQuery来设置隐藏字段:
<脚本类型=文/ JavaScript的>
$(函数(){
$(#SelectedVender)。在(变,功能{
$(#SelectedvendorText)VAL($(本)的.text());
});
});
< / SCRIPT>@ Html.DropDownListFor(M = GT; m.SelectedVender,Model.Vendor,选择Manufacurer)
@ Html.HiddenFor(M = GT; m.SelectedvendorText)
I have dropdownlist, which i have filled from database. Now i need to get the selected value in Controller do some manipulation. But not getting the idea. Code which i have tried.
Model
public class MobileViewModel
{
public List<tbInsertMobile> MobileList;
public SelectList Vendor { get; set; }
}
Controller
public ActionResult ShowAllMobileDetails()
{
MobileViewModel MV = new MobileViewModel();
MV.MobileList = db.Usp_InsertUpdateDelete(null, "", "", null, "", 4, MergeOption.AppendOnly).ToList();
MV.Vendor = new SelectList(db.Usp_VendorList(), "VendorId", "VendorName");
return View(MV);
}
[HttpPost]
public ActionResult ShowAllMobileDetails(MobileViewModel MV)
{
string strDDLValue = ""; // Here i need the dropdownlist value
return View(MV);
}
View
<table>
<tr>
<td>Mobile Manufacured</td>
<td>@Html.DropDownList("ddlVendor", Model.Vendor, "Select Manufacurer") </td>
</tr>
<tr>
<td>
</td>
<td>
<input id="Submit1" type="submit" value="search" />
</td>
</tr>
</table>
1st Approach (via Request or FormCollection):
You can read it from Request
using Request.Form
, your dropdown name is ddlVendor
so pass ddlVendor
key in the formCollection to get its value that is posted by form:
string strDDLValue = Request.Form["ddlVendor"].ToString();
or Use FormCollection
:
[HttpPost]
public ActionResult ShowAllMobileDetails(MobileViewModel MV,FormCollection form)
{
string strDDLValue = form["ddlVendor"].ToString();
return View(MV);
}
2nd Approach (Via Model):
If you want with Model binding then add a property in Model:
public class MobileViewModel
{
public List<tbInsertMobile> MobileList;
public SelectList Vendor { get; set; }
public string SelectedVender {get;set;}
}
and in View:
@Html.DropDownListFor(m=>m.SelectedVender , Model.Vendor, "Select Manufacurer")
and in Action:
[HttpPost]
public ActionResult ShowAllMobileDetails(MobileViewModel MV)
{
string SelectedValue = MV.SelectedVendor;
return View(MV);
}
UPDATE:
If you want to post the text of selected item as well, you have to add a hidden field and on drop down selection change set selected item text in the hidden field:
public class MobileViewModel
{
public List<tbInsertMobile> MobileList;
public SelectList Vendor { get; set; }
public string SelectVendor {get;set;}
public string SelectedvendorText { get; set; }
}
use jquery to set hidden field:
<script type="text/javascript">
$(function(){
$("#SelectedVender").on("change", function {
$("#SelectedvendorText").val($(this).text());
});
});
</script>
@Html.DropDownListFor(m=>m.SelectedVender , Model.Vendor, "Select Manufacurer")
@Html.HiddenFor(m=>m.SelectedvendorText)
这篇关于如何获得的DropDownList的SelectedValue的控制器在MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!