Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC3 Entity Framework 4.1RC how does @Html.DropDownListFor actually work?
    primarykey
    data
    text
    <p>OK, I've been over Google and StackOverflow and ASP.net - am I really the only person who doesn't get this?</p> <p>Rough problem = I have an Employee entity that references an Office entity. I was previously able to create new employees (beat me with a hammer for forgetting how that code worked) but now I just can't create an employee nor edit an existing one.</p> <p>Now, here's what I learned; 1) Make sure you add the Offices list to the ViewBag at every step 2) That includes a failed POST/edit; call the function to re-populate the ViewBag with the Offices list 3) I think(!!) that you always want to set the Employee.Office, not the Employee.Office.OfficeID; the latter leads to "is part of the object's key information and cannot be modified" errors</p> <p>So, what I have is;</p> <p>A controller that has the following method;</p> <pre><code> private void AddOfficesToViewBag() { Dictionary&lt;string, Office&gt; list = new Dictionary&lt;string, Office&gt;(); foreach (Office office in company.GetAllOffices()) list.Add(office.ToString(), office); SelectList items = new SelectList(list, "Value", "Key"); ViewBag.OfficeList = items; } </code></pre> <p>Create pair looking like;</p> <pre><code> public ActionResult Create() { if (company.Offices.Count() &lt; 1) return RedirectToAction("Create", "Office", (object) "You need to create one or more offices first"); AddOfficesToViewBag(); return View(new Employee()); } // // POST: /Employee/Create [HttpPost] public ActionResult Create(Employee emp) { if (TryUpdateModel&lt;Employee&gt;(emp)) { company.Employees.Add(emp); company.SaveChanges(); return RedirectToAction("Index"); } else { AddOfficesToViewBag(); return View(emp); } } </code></pre> <p>and an Edit pair that looks like;</p> <pre><code> public ActionResult Edit(int id) { Employee emp = company.Employees.Single(e =&gt; e.EmployeeID == id); AddOfficesToViewBag(); return View(emp); } // // POST: /Employee/Edit/5 [HttpPost] public ActionResult Edit(int id, FormCollection collection) { Employee emp = company.Employees.Single(e =&gt; e.EmployeeID == id); if (TryUpdateModel(emp)) { company.SaveChanges(); return RedirectToAction("Index"); } else { AddOfficesToViewBag(); return View(emp); } } </code></pre> <p>I'll pick the Edit View, which is pretty much the same as the Create View;</p> <p>@using (Html.BeginForm()) { @Html.ValidationSummary(true) Employee</p> <pre><code> @Html.HiddenFor(model =&gt; model.EmployeeID) &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Office) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.DropDownListFor(model =&gt; model.Office, (SelectList) ViewBag.OfficeList) @Html.ValidationMessageFor(model =&gt; model.Office) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Name) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Name) @Html.ValidationMessageFor(model =&gt; model.Name) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Age) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Age) @Html.ValidationMessageFor(model =&gt; model.Age) &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Save" /&gt; &lt;/p&gt; &lt;/fieldset&gt; </code></pre> <p>}</p> <p>I would say that the Edit, in particular, looks almost there. It manages to bind to the Employee object passed in and sets the dropdown to the appropriate entry. Viewing the original HTML source shows that the output value is the Office.ToString() value. The odd thing to me is that some magic is happening that binds Employee->Office to the correct entry, which makes the Edit view work, but there is no corresponding conversion of the selected item (a string, aka object->ToString()) to the original list.</p> <p>This seems so basic (MVC / EF4 / DropDownList) that I feel I'm missing something incredibly fundamental.</p> <p>All thoughts appreciated. Regards Scott</p>
    singulars
    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.
 

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