如何在MVC控制器中获取DropDownList的SelectedValue

我有dropdownlist,我已经从数据库中填充。 现在我需要得到控制器中选定的值做一些操作。 但没有得到这个想法。 代码,我已经尝试。

模型

public class MobileViewModel { public List<tbInsertMobile> MobileList; public SelectList Vendor { get; set; } } 

调节器

  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); } 

视图

  <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> 

第一种方法(通过请求或FormCollection):

您可以使用Request.FormRequest读取它,您的下拉列表名称是ddlVendor因此在formCollection中传递ddlVendor键以获取由form发布的值:

 string strDDLValue = Request.Form["ddlVendor"].ToString(); 

或使用FormCollection

 [HttpPost] public ActionResult ShowAllMobileDetails(MobileViewModel MV,FormCollection form) { string strDDLValue = form["ddlVendor"].ToString(); return View(MV); } 

第二种方法(通过模型):

如果你想用模型绑定,然后在模型中添加一个属性:

 public class MobileViewModel { public List<tbInsertMobile> MobileList; public SelectList Vendor { get; set; } public string SelectedVendor {get;set;} } 

并在视图中:

 @Html.DropDownListFor(m=>m.SelectedVendor , Model.Vendor, "Select Manufacurer") 

并在行动:

 [HttpPost] public ActionResult ShowAllMobileDetails(MobileViewModel MV) { string SelectedValue = MV.SelectedVendor; return View(MV); } 

更新:

如果您还想发布所选项目的文本,则必须添加隐藏字段,并在下拉select更改中将所选项目文本设置在隐藏字段中:

 public class MobileViewModel { public List<tbInsertMobile> MobileList; public SelectList Vendor { get; set; } public string SelectVendor {get;set;} public string SelectedvendorText { get; set; } } 

使用jQuery来设置隐藏字段:

 <script type="text/javascript"> $(function(){ $("#SelectedVendor").on("change", function { $("#SelectedvendorText").val($(this).text()); }); }); </script> @Html.DropDownListFor(m=>m.SelectedVendor , Model.Vendor, "Select Manufacurer") @Html.HiddenFor(m=>m.SelectedvendorText) 

模型

与性别字段非常基本的模型。 GetGenderSelectItems()返回填充DropDownList所需的select项。

 public enum Gender { Male, Female } public class MyModel { public Gender Gender { get; set; } public static IEnumerable<SelectListItem> GetGenderSelectItems() { yield return new SelectListItem { Text = "Male", Value = "Male" }; yield return new SelectListItem { Text = "Female", Value = "Female" }; } } 

视图

请确保您将@Html.DropDownListFor在表单标记中。

 @model MyModel @using (Html.BeginForm("MyController", "MyAction", FormMethod.Post) { @Html.DropDownListFor(m => m.Gender, MyModel.GetGenderSelectItems()) <input type="submit" value="Send" /> } 

调节器

您的.cshtml Razor视图名称应与控制器操作名称相同,并且文件夹名称应与控制器名称匹配,例如Views\MyController\MyAction.cshtml

 public class MyController : Controller { public ActionResult MyAction() { // shows your form when you load the page return View(); } [HttpPost] public ActionResult MyAction(MyModel model) { // the value is received in the controller. var selectedGender = model.Gender; return View(model); } } 

走得更远

现在让我们把它强制types化和枚举独立:

 var genderSelectItems = Enum.GetValues(typeof(Gender)) .Cast<string>() .Select(genderString => new SelectListItem { Text = genderString, Value = genderString, }).AsEnumerable(); 

如果你正在寻找一些轻量级的东西,我会在你的动作中附加一个参数。

 [HttpPost] public ActionResult ShowAllMobileDetails(MobileViewModel MV, string ddlVendor) { string strDDLValue = ddlVendor; // Of course, this becomes silly. return View(MV); } 

现在你的代码中发生了什么,你是在将“ddlVendor”的第一个stringparameter passing给Html.DropDownList ,并告诉MVC框架创build一个name “ddlVendor”的<select>元素。 当用户提交表单客户端时,它将包含该键的值。

当MVC尝试将该请求parsing为MV ,它将查找MobileListVendor ,但找不到它,因此不会填充。 通过添加此参数,或使用FormCollection作为另一个答案已经build议,您要求MVC专门查找具有该名称的表单元素,所以它应该填充参数值与张贴值。

使用SelectList绑定@HtmlDropdownListFor并在其中指定selectedValue参数。

http://msdn.microsoft.com/en-us/library/dd492553(v=vs.108).aspx

例如:你可以这样做获取venderid

 @Html.DropDownListFor(m => m.VendorId,Model.Vendor) public class MobileViewModel { public List<tbInsertMobile> MobileList; public SelectList Vendor { get; set; } public int VenderID{get;set;} } [HttpPost] public ActionResult Action(MobileViewModel model) { var Id = model.VenderID; 

谢谢 – 这有助于我更好地理解和解决我遇到的问题。 提供的JQuery提供的selectedItem的文本没有为我工作,我改变了

 $(function () { $("#SelectedVender").on("change", function () { $("#SelectedvendorText").val($(**"#SelectedVender option:selected"**).text()); }); }); 

我在asp.NETrazorC#中遇到同样的问题

我有一个ComboBoxEventMessage填充标题,我想显示此消息的内容与其选定的值,以显示在标签或TextField或任何其他Control

我的ComboBox是这样填充的:

  @Html.DropDownList("EventBerichten", new SelectList(ViewBag.EventBerichten, "EventBerichtenID", "Titel"), new { @class = "form-control", onchange = "$(this.form).submit();" }) 

在我的EventController我有一个function去页面,我想显示我的ComboBox (这是一个不同的模型types,所以我不得不使用部分视图)?

从索引到页面加载部分视图的函数:

  public ActionResult EventDetail(int id) { Event eventOrg = db.Event.Include(s => s.Files).SingleOrDefault(s => s.EventID == id); // EventOrg eventOrg = db.EventOrgs.Find(id); if (eventOrg == null) { return HttpNotFound(); } ViewBag.EventBerichten = GetEventBerichtenLijst(id); ViewBag.eventOrg = eventOrg; return View(eventOrg); } 

局部视图的function在这里:

  public PartialViewResult InhoudByIdPartial(int id) { return PartialView( db.EventBericht.Where(r => r.EventID == id).ToList()); } 

填充EventBerichten的函数:

  public List<EventBerichten> GetEventBerichtenLijst(int id) { var eventLijst = db.EventBericht.ToList(); var berLijst = new List<EventBerichten>(); foreach (var ber in eventLijst) { if (ber.EventID == id ) { berLijst.Add(ber); } } return berLijst; } 

partialView模型看起来像这样:

 @model IEnumerable<STUVF_back_end.Models.EventBerichten> <table> <tr> <th> EventID </th> <th> Titel </th> <th> Inhoud </th> <th> BerichtDatum </th> <th> BerichtTijd </th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.EventID) </td> <td> @Html.DisplayFor(modelItem => item.Titel) </td> <td> @Html.DisplayFor(modelItem => item.Inhoud) </td> <td> @Html.DisplayFor(modelItem => item.BerichtDatum) </td> <td> @Html.DisplayFor(modelItem => item.BerichtTijd) </td> </tr> } </table> 

VIEUW :这是用于在视图中获取我的输出的脚本

 <script type="text/javascript"> $(document).ready(function () { $("#EventBerichten").change(function () { $("#log").ajaxError(function (event, jqxhr, settings, exception) { alert(exception); }); var BerichtSelected = $("select option:selected").first().text(); $.get('@Url.Action("InhoudByIdPartial")', { EventBerichtID: BerichtSelected }, function (data) { $("#target").html(data); }); }); }); </script> @{ Html.RenderAction("InhoudByIdPartial", Model.EventID); } <fieldset> <legend>Berichten over dit Evenement</legend> <div> @Html.DropDownList("EventBerichten", new SelectList(ViewBag.EventBerichten, "EventBerichtenID", "Titel"), new { @class = "form-control", onchange = "$(this.form).submit();" }) </div> <br /> <div id="target"> </div> <div id="log"> </div> </fieldset>