Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC3 DropDownListFor - a simple example?
    primarykey
    data
    text
    <p>I'm having trouble with <code>DropDownListFor</code> in my MVC3 app. I was able to use StackOverflow to figure out how to get them to appear on the View, but now I don't know how to capture the values in its corresponding properties on the View Model when it's submitted. In order to get this to work I had to create an inner class that had an ID and a value property, then I had to use an <code>IEnumerable&lt;Contrib&gt;</code> to satisfy the <code>DropDownListFor</code> parameter requirements. Now, however, how is MVC FW supposed to map the value that is selected on this drop-down back into the simple string property on my view model? </p> <pre><code>public class MyViewModelClass { public class Contrib { public int ContribId { get; set; } public string Value { get; set; } } public IEnumerable&lt;Contrib&gt; ContribTypeOptions = new List&lt;Contrib&gt; { new Contrib {ContribId = 0, Value = "Payroll Deduction"}, new Contrib {ContribId = 1, Value = "Bill Me"} }; [DisplayName("Contribution Type")] public string ContribType { get; set; } } </code></pre> <p>In my View I place the dropdown on the page like this:</p> <pre><code>&lt;div class="editor-label"&gt; @Html.LabelFor(m =&gt; m.ContribType) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.DropDownListFor(m =&gt; m.ContribTypeOptions.First().ContribId, new SelectList(Model.ContribTypeOptions, "ContribId", "Value")) &lt;/div&gt; </code></pre> <p>When I submit the form the <code>ContribType</code> is (of course) null. </p> <p>What is the correct way to do this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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