Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC 3 Drop Down List Not Binding to Model
    primarykey
    data
    text
    <p>In my application I have a drop down list to represent different choices. Note that <code>Paragraph</code> is a model and the section is just one field in the model.</p> <pre><code>@Html.DropDownList("Sections") </code></pre> <p>And here is my controller.</p> <pre><code>public ActionResult Edit(int id) { var paragraph = db.Paragraphs.Find(id); ViewBag.Sections = new SelectList( db.Sections.Select(s =&gt; new { s.ID, s.Name }), "ID", "Name", paragraph.SectionID ); return View(paragraph); } [HttpPost] public ActionResult Edit(Paragraph paragraph, HttpPostedFileBase document) { if (ModelState.IsValid) { // Do some stuff. } ViewBag.Sections = new SelectList( db.Sections.Select(s =&gt; new { s.ID, s.Name }), "ID", "Name", paragraph.SectionID ); return View(paragraph); } </code></pre> <p>When I submit the form though the drop down list is not bound to the model. Causing <code>ModelState.IsValid</code> to be false and making my life horrible. Any suggestions?</p> <p><strong>EDIT</strong>: When I submit the form I get the following error:</p> <pre><code>There is no ViewData item of type 'IEnumerable&lt;SelectListItem&gt;' that has the key 'Sections'. </code></pre> <p><strong>EDIT</strong>: It appears that I only get the preceding error when I try to submit the file.</p> <p><strong>EDIT</strong>: Model</p> <pre><code>public class Paragraph { public int ID { get; set; } [Required] public int Major { get; set; } [Required] public int Minor { get; set; } [Required(ErrorMessage = "Name is required")] [StringLength(4000)] public string Name { get; set; } public int SectionID { get; set; } public virtual Section Section { get; set; } } </code></pre> <p>Form: (It's a lot.)</p> <pre><code>&lt;form class="form-horizontal" action="/Paragraph/Edit" method="post" enctype="multipart/form-data"&gt; &lt;fieldset&gt; &lt;div class="control-group"&gt; &lt;label class="control-label" for="section"&gt;Section&lt;/label&gt; &lt;div class="controls"&gt; @Html.DropDownList("Sections") &lt;/div&gt; &lt;/div&gt; &lt;div class="control-group"&gt; &lt;label class="control-label" for="major"&gt;Major&lt;/label&gt; &lt;div class="controls"&gt; &lt;input type="number" class="input-large" name="major" value="@Model.Major" /&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="control-group"&gt; &lt;label class="control-label" for="minor"&gt;Minor&lt;/label&gt; &lt;div class="controls"&gt; &lt;input type="number" class="input-large" name="minor" value="@Model.Minor" /&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="control-group"&gt; &lt;label class="control-label" for="name"&gt;Name&lt;/label&gt; &lt;div class="controls"&gt; &lt;input type="text" class="input-large" name="name" value="@Model.Name" /&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="control-group"&gt; &lt;label class="control-label" for="document"&gt;Document&lt;/label&gt; &lt;div class="controls"&gt; &lt;input type="file" class="input-file" name="document" /&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="form-actions"&gt; &lt;input type="submit" class="btn btn-primary" value="Save" /&gt; &lt;a class="btn" href="/Paragraph/Show/@Model.ID"&gt;Cancel&lt;/a&gt; &lt;/div&gt; &lt;/fieldset&gt; &lt;/form&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
    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