![](/img/trans.png)
[英]ASP.NET Core MVC SelectList not setting selectedValue
[英]ASP.NET MVC selectlist selectedvalue not working
我正在使用三個不同的選擇Selectlist
來填充三個不同的下拉列表。 區域,學科和轉變。
我面臨的問題是它適用於第一個,但不適用於其他兩個。 在表單上, selectedvalue
適用於該區域,但是對於紀律和轉移,它將選擇列表中的第一個
List<SelectListItem> ShiftEdit = db.Shifts
.OrderBy(s => s.Site.SiteName)
.Select(s => new SelectListItem
{
Value = s.ShiftID.ToString(),
Text = s.Site.SiteName + " " + "||" + " " + s.Shift1
}).ToList();
ViewBag.ShiftEdit = new SelectList(ShiftEdit, "Value", "Text", employee.ShiftID);
List<SelectListItem> AreaEdit = db.Areas
.OrderBy(s => s.Site.SiteName)
.Select(s => new SelectListItem
{
Value = s.AreaID.ToString(),
Text = s.Site.SiteName + " " + "||" + " " + s.Area1
}).ToList();
ViewBag.AreaEdit = new SelectList(AreaEdit, "Value", "Text", employee.AreaID);
List<SelectListItem> DisciplineEdit = db.Disciplines
.OrderBy(s => s.Site.SiteName)
.Select(s => new SelectListItem
{
Value = s.DisciplineID.ToString(),
Text = s.Site.SiteName + " " + "||" + " " + s.Discipline1
}).ToList();
ViewBag.DisciplineEdit = new SelectList(DisciplineEdit, "Value", "Text", employee.DisciplineID);
風景:
<div class="form-group">
@Html.LabelFor(model => model.ShiftID, "Shift", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("ShiftID", (IEnumerable<SelectListItem>)ViewBag.ShiftEdit, htmlAttributes: new { @class = "form-control", @style = "width:400px" })
@Html.ValidationMessageFor(model => model.ShiftID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AreaID, "Area", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("AreaID", (IEnumerable<SelectListItem>)ViewBag.AreaEdit, htmlAttributes: new { @class = "form-control", @style = "width:400px" })
@Html.ValidationMessageFor(model => model.AreaID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DisciplineID, "Discipline", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("DisciplineID", (IEnumerable<SelectListItem>)ViewBag.DisciplineEdit, htmlAttributes: new { @class = "form-control", @style = "width:400px" })
@Html.ValidationMessageFor(model => model.DisciplineID, "", new { @class = "text-danger" })
</div>
</div>
所以我錯誤地沒有刪除似乎引起問題的原始腳手架Viewbag。
我刪除了它們,現在一切正常
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.