简体   繁体   中英

Cannot get a dropdown list to pass data from view to controller?

I have set up an MVC web app using entity framework for a leave request system. The model, RequestModel, has a value LeaveType, which I would like to offer the user to select from a drop down list. The code for the LeaveType input on theview page is as follows:

    <div class="form-group">
        @Html.Label("Leave Type", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("LeaveTypes")
        </div>
    </div>

The types of leave are passed in a list of strings through the viewbag, and display fine when viewing the web page and allow you to select one. However, when debugging I can see that whatever you select never passes through to the HttpPost Create method, instead just displaying the value as null. The post create opens as such with the following Bind command:

    // POST: Request/Create
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "ID,LeaveType,FullName,Email,StartDate,EndDate,LeaveLength,Details,RequestDate,ReplyDate,Accepted,Special")] RequestModel requestModel)
    {

I would greatly appreciate any ideas as to what I might have missed. I can add any other parts of the code if they would help get to the root of the problem, just let me know.

You create a dropdown with name LeaveTypes but bind it as LeaveType . Change the name of dropdown accordingly:

@Html.DropDownList("LeaveType")

...or use

Html.DropDownListFor(m => m.LeaveType, ...)

method

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