我不明白为什么我的编辑页面无法在我的下拉列表中获取所选值。
我的创建功能工作正常,但我的编辑页面未在我的下拉列表中显示所选值。我试图通过使用Viewbags获取所选的值,如果我正在调试,则存在id。
这是我的Edit()
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
SeminarErstellen seminarerstellen = db.SeminarErstellen.Find(id);
if (seminarerstellen == null)
{
return HttpNotFound();
}
ViewBag.FileUploadId = new SelectList(db.FileUpload, "FileUploadId", "FilePath", seminarerstellen.FileUploadId);
ViewBag.LehrbeauftragterId = new SelectList(db.Lehrbeauftragter, "LehrbeauftragterId", "Vorname", seminarerstellen.LehrbeauftragterId);
ViewBag.SeminarStatiId = new SelectList(db.SeminarStati, "SeminarStatiId", "StatusTyp", seminarerstellen.SeminarStatiId);
ViewBag.SeminarTypId = new SelectList(db.SeminarTyp, "SeminarTypId", "Seminarbezeichnung", seminarerstellen.SeminarTypId);
return View(seminarerstellen);
}
这是我的观点的DropdownList:问题是,它总是选择我的下拉列表的第一项而不是选择的......我希望你理解......
<div class="form-group">
@Html.LabelFor(model => model.SeminarTypId, "SeminarTypId", new { @class = "control-label col-md-2" })
<div class="col-md-3">
@* This Dropdown is selecting the right value but without Bootstrap!*@
@*@Html.DropDownList("SeminarTypId", String.Empty)*@
@* This line is selecting the wrong item, its selecting the first item of the list*@
@Html.DropDownList("SeminarTypId", (SelectList)ViewBag.SeminarTypId, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.SeminarTypId)
</div>
</div>
答案 0 :(得分:0)
删除您的混乱
......................
<强> 解决方案 强>
问题出在您的ViewBag属性名称中。因为它与模型中的属性相同,所以不起作用。
YourCode
Controller
ViewBag.SeminarTypId = new SelectList(db.SeminarTyp, "SeminarTypId", minarbezeichnung",seminarerstellen.SeminarTypId);
View
@Html.DropDownList("SeminarTypId", (SelectList)ViewBag.SeminarTypId, new { @class = "form-control" })
Modification
Controller
ViewBag.SeminarTypId123 = new SelectList(db.SeminarTyp, "SeminarTypId", minarbezeichnung",seminarerstellen.SeminarTypId);
View
@Html.DropDownList("SeminarTypId", (SelectList)ViewBag.SeminarTypId123, new { @class = "form-control" })