如何从View到Controller获取DropdownList值(How to get DropdownList value from View to Controller)
喜欢标题
我一步一步地举例说明
我可以将SelectItemList从控制器传递给View来执行DropDownList
但我无法从View到Controller获取值
有人可以教我如何使用MVC
这是我的控制器
var Course = getCourseList.getCourse(); if (Course.Any()) { List<SelectListItem> items = new List<SelectListItem>(); foreach (var course in Course) { items.Add(new SelectListItem() { Text = course.RC_NAME, Value = course.RC_MAJORCODE, }); } ViewBag.SelectList = items; }这是我的观点
@using YTDT_OAuth.Models @model List<CourseInfo> @using (Html.BeginForm("CourseSave", "Home")) { @Html.AntiForgeryToken() <div class="form-horizontal"> <table class="table"> <tr> <th scope="row">first</th> <td>@Html.DropDownList("SelectList", null, "default", new {@id = "a" })</td> @foreach (var item in Model) { <p>@item.RC_MAJORCODE</p> } </tr> </table> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Save" class="btn btn-default" /> </div> </div> </div> }我想在这个控制器中做点什么
[HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public ActionResult CourseSave(CourseInfo CourseDataz) { // the value is received in the controller. return View(); }Like Title
I step by step with examples
I can pass SelectItemList to View from controller to do DropDownList
But I Can't get a value from View to Controller
Have someone can teach me how to do with MVC
this is my Controller
var Course = getCourseList.getCourse(); if (Course.Any()) { List<SelectListItem> items = new List<SelectListItem>(); foreach (var course in Course) { items.Add(new SelectListItem() { Text = course.RC_NAME, Value = course.RC_MAJORCODE, }); } ViewBag.SelectList = items; }And this is my View
@using YTDT_OAuth.Models @model List<CourseInfo> @using (Html.BeginForm("CourseSave", "Home")) { @Html.AntiForgeryToken() <div class="form-horizontal"> <table class="table"> <tr> <th scope="row">first</th> <td>@Html.DropDownList("SelectList", null, "default", new {@id = "a" })</td> @foreach (var item in Model) { <p>@item.RC_MAJORCODE</p> } </tr> </table> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Save" class="btn btn-default" /> </div> </div> </div> }and I want to do something in this controller
[HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public ActionResult CourseSave(CourseInfo CourseDataz) { // the value is received in the controller. return View(); }最满意答案
我个人喜欢使用强类型模型而不是ViewBag。 ViewData和ViewBag是动态的,它们会抛出运行时错误而不是编译时错误。
示例代码
模型
public class CourseInfo { public string SelectedMajorCode { get; set; } public IList<SelectListItem> AvailableMajorNames { get; set; } public CourseInfo() { AvailableMajorNames = new List<SelectListItem>(); } }视图
@model DemoMvc.Models.CourseInfo @using (Html.BeginForm("Index", "Home")) { @Html.DropDownListFor(m => m.SelectedMajorCode, Model.AvailableMajorNames) <input type="submit" value="Submit"/> }调节器
public class HomeController : Controller { public ActionResult Index() { var model = new CourseInfo { AvailableMajorNames = GetColorListItems() }; return View(model); } [HttpPost] public ActionResult Index(CourseInfo model) { if (ModelState.IsValid) { var majorCode = model.SelectedMajorCode; return View("Success"); } // If we got this far, something failed, redisplay form // ** IMPORTANT : Fill AvailableMajorNames again; // otherwise, DropDownList will be blank. ** model.AvailableMajorNames = GetMajorListItems(); return View(model); } private IList<SelectListItem> GetMajorListItems() { // This could be from database. return new List<SelectListItem> { new SelectListItem {Text = "One", Value = "1"}, new SelectListItem {Text = "Two", Value = "2"} }; } }I personally like to use strongly-typed Model instead of ViewBag. ViewData and ViewBag are dynamic, and they throw run-time error rather than compile-time error.
Sample Code
Model
public class CourseInfo { public string SelectedMajorCode { get; set; } public IList<SelectListItem> AvailableMajorNames { get; set; } public CourseInfo() { AvailableMajorNames = new List<SelectListItem>(); } }View
@model DemoMvc.Models.CourseInfo @using (Html.BeginForm("Index", "Home")) { @Html.DropDownListFor(m => m.SelectedMajorCode, Model.AvailableMajorNames) <input type="submit" value="Submit"/> }Controller
public class HomeController : Controller { public ActionResult Index() { var model = new CourseInfo { AvailableMajorNames = GetColorListItems() }; return View(model); } [HttpPost] public ActionResult Index(CourseInfo model) { if (ModelState.IsValid) { var majorCode = model.SelectedMajorCode; return View("Success"); } // If we got this far, something failed, redisplay form // ** IMPORTANT : Fill AvailableMajorNames again; // otherwise, DropDownList will be blank. ** model.AvailableMajorNames = GetMajorListItems(); return View(model); } private IList<SelectListItem> GetMajorListItems() { // This could be from database. return new List<SelectListItem> { new SelectListItem {Text = "One", Value = "1"}, new SelectListItem {Text = "Two", Value = "2"} }; } }更多推荐
发布评论