帮助我创建数据库中所有 Jefe 的下拉列表!
我有以下表结构:
我试图在编辑区域时填充所有 Jefe 的组合框。这意味着我可以改变谁负责一个区域。
这是我的 AreaController.cs 代码:
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);
}
}
请注意,.FindAllJefes() 返回一个 IQueryable<>收藏。
现在,在我的 Edit.aspx 文件中,我有以下内容:
<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>
我收到此错误:
没有 ViewData 类型的项 'IEnumerable' 具有 密钥“IDJefe”。
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'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据MSDN:
jefe。当传递给 SelectList 构造函数时,FindAllJefes()
应该是一个 IEnumerable。您的参数应为jefe.FindAllJefes().ToList()
Per MSDN :
jefe.FindAllJefes()
should be an IEnumerable when passed to a SelectList ctor. Your argument which should readjefe.FindAllJefes().ToList()
如果没有看到您的自定义类是如何设计的,这是一个困难的问题,但只是冒险猜测:
我不知道您的 SelectList 类是如何设置的,但是(也许)您使用“Nombre”作为 ID 和显示值(???)。
因此,您的选择列表正在寻找名为“IDJefe”的键,但没有找到它,因为您使用了“Nombre”两次。
Without seeing how your custom classes are designed, this is a tough one, but just to hazard a guess:
I don't know how your SelectList class is setup, but (maybe) you're using "Nombre" as the ID AND the display value (???).
So your select list is looking for a key called "IDJefe", and not finding it, because you used "Nombre" twice.