Hi guys, I have the following table structure:
I'm trying to populate a combobox of all the Jefes when editing an Area. Meaning I can change who's in charge of an area.
Here is my AreaController.cs code:
public ActionResult Edit(int id)
{
Area area = areaRepository.GetArea(id);
JefeRepository jefe = new JefeRepository();
ViewData["Jefes"] = new SelectList(jefe.FindAllJefes(), "Nombre", "Nombre");
return View(area);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection formValues)
{
Area area = areaRepository.GetArea(id);
try
{
UpdateModel(area);
areaRepository.Save();
return RedirectToAction("Details", new { id = area.ID });
}
catch (Exception)
{
foreach (var issue in area.GetRuleViolations())
{
ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
}
return View(area);
}
}
Note that .FindAllJefes() returns an IQueryable<> collection.
Now in my Edit.aspx file I have the following:
<fieldset>
<legend>Informacion Detallada de Area | <%: Model.Nombre %></legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.Nombre) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Nombre) %>
<%: Html.ValidationMessageFor(model => model.Nombre) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.IDJefe) %>
</div>
<div class="editor-field">
<%: Html.DropDownList("IDJefe", (SelectList)ViewData["Jefes"], "(Select Jefe)") %>
<%: Html.ValidationMessageFor(model => model.IDJefe) %>
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
I receive this error:
There is no ViewData item of type 'IEnumerable' that has the key 'IDJefe'.