如何从View到Controller获取DropdownList值(How to get DropdownList value from View to Controller)

编程入门 行业动态 更新时间:2024-10-12 08:25:55
如何从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"} }; } }

更多推荐

本文发布于:2023-07-30 14:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338849.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:View   Controller   DropdownList

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!