ASP.NET MVC 选择列表选定值不起作用



我正在使用三种不同的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>

所以我错误地没有删除原始脚手架的 Viewbags,这似乎导致了问题。

我删除了它们,现在一切正常

相关内容

  • 没有找到相关文章

最新更新