Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The issue you are running into is that MVC does not know how to translate the selected value of the drop down list (which is a string) to the object Member.</p> <p>What you should do is have a selectedValue property that is used to set the value in the dropdown and retrieve the returned value.</p> <p>New Customer Class:</p> <pre><code>public class Customer { public string name { get; set; } public List&lt;SelectListItem&gt; members { get; set; } public string selectedValue { get; set; } public Member selected { get; set; } } </code></pre> <p>Updated Dropdown list control:</p> <pre><code>@Html.DropDownListFor(model =&gt; model.selectedValue, Model.members, "--Select--") </code></pre> <p>This will return the selected value from the dropdown list into the property selectedValue.</p> <p>The reason that your List of members is returned null is because HTML does not return the options in a drop down list, it only returns the selected value. So when the information comes into the method, it is only given the values of the input forms.</p> <p>If you want to see what information gets sent back to the server you can use the developer console and capture the return Http request</p> <p>and/or</p> <p>You can add <code>FormCollection collection</code> to the parameters of the controller action to see what information MVC is using to build the objects it passes to the methods.</p> <pre><code>[HttpPost] public ActionResult Index(Customer customer, FormCollection collection) { return View(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload