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

时间:2019-06-20 14:44:43

标签: c# html asp.net-mvc selectlist selectedvalue

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

1 个答案:

答案 0 :(得分:0)

所以我错误地没有删除似乎引起问题的原始脚手架Viewbag。

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