简体   繁体   中英

How to get DropdownList value from View to Controller

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM