【问题标题】:ASP.NET MVC selectlist selectedvalue not workingASP.NET MVC 选择列表 selectedvalue 不起作用
【发布时间】:2019-06-20 14:44:43
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-25
      相关资源
      最近更新 更多