我在编辑表单中的应用程序中有一个下拉列表。 但在某些地方我错了。
HomeController.cs
ViewData["RealizatorID"] = from r in db.tGlavnas
select new SelectListItem { Text = r.Realizator.ImeRealizatora,
Value = r.Realizator.ImeRealizatora, Selected = (r.RealizatorID == id) };
Edit.aspx
<div class="editor-label">
<%: Html.LabelFor(model => model.RealizatorID) %>
</div>
<div class="editor-field">
<%: Html.DropDownList("RealizatorID")%>
<%: Html.ValidationMessageFor(model => model.RealizatorID) %>
</div>
在Edit.aspx页面中记录更改后,向我报告错误:
没有类型为“IEnumerable”的ViewData项具有“RealizatorID”键。
答案 0 :(得分:0)
我建议你做以下事情:
控制器:
ViewData["SelRealizator"] = from r in db.tGlavnas
select new SelectListItem { Text = r.Realizator.ImeRealizatora,
Value = r.Realizator.ImeRealizatora, Selected = (r.RealizatorID == id) };
查看:
<div class="editor-label">
<%: Html.LabelFor(model => model.RealizatorID) %>
</div>
<div class="editor-field">
<%: Html.DropDownListFor(model => model.RealizatorID, ViewData["SelRealizator"] as IEnumerable<SelectListItem>)%>
<%: Html.ValidationMessageFor(model => model.RealizatorID) %>
</div>
答案 1 :(得分:0)
完整代码:
public ActionResult Edit(int id)
{
var log = db.tGlavnas.Single(lo => lo.RedniBroj == id);
ViewData["SelRealizator"] = from r in db.tGlavnas select new SelectListItem { Text = r.Realizator.ImeRealizatora, Value = r.Realizator.ImeRealizatora, Selected = (r.RealizatorID == id) };
return View(log);
}
我还没有写Post方法。
答案 2 :(得分:0)
Edit.aspx
<h2>Edit</h2>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.RedniBroj) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.RedniBroj) %>
<%: Html.ValidationMessageFor(model => model.RedniBroj) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.DatumPrijema) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.DatumPrijema) %>
<%: Html.ValidationMessageFor(model => model.DatumPrijema) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.DatumPredaje) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.DatumPredaje) %>
<%: Html.ValidationMessageFor(model => model.DatumPredaje) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.BrojID) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.BrojID) %>
<%: Html.ValidationMessageFor(model => model.BrojID) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.PredmetJavneNabavke) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.PredmetJavneNabavke) %>
<%: Html.ValidationMessageFor(model => model.PredmetJavneNabavke) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.VrstaID) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.VrstaID) %>
<%: Html.ValidationMessageFor(model => model.VrstaID) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.TipSredstvaID) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.TipSredstvaID) %>
<%: Html.ValidationMessageFor(model => model.TipSredstvaID) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.ProcenjenaVrednost) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.ProcenjenaVrednost) %>
<%: Html.ValidationMessageFor(model => model.ProcenjenaVrednost) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.PoKontuID) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.PoKontuID) %>
<%: Html.ValidationMessageFor(model => model.PoKontuID) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.RealizatorID) %>
</div>
<div class="editor-field">
<%: Html.DropDownListFor(model => model.RealizatorID, ViewData["SelRealizator"] as
IEnumerable<SelectListItem>)%>
<%: Html.ValidationMessageFor(model => model.RealizatorID) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.PonudjacID) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.PonudjacID) %>
<%: Html.ValidationMessageFor(model => model.PonudjacID) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Iznos) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Iznos, String.Format("{0:F}", Model.Iznos)) %>
<%: Html.ValidationMessageFor(model => model.Iznos) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.BrojRacuna) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.BrojRacuna) %>
<%: Html.ValidationMessageFor(model => model.BrojRacuna) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.DatumIsplate) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.DatumIsplate, String.Format("{0:g}", Model.DatumIsplate)) %>
<%: Html.ValidationMessageFor(model => model.DatumIsplate) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.JavnaNabSprovPostID) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.JavnaNabSprovPostID) %>
<%: Html.ValidationMessageFor(model => model.JavnaNabSprovPostID) %>
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>