Search code examples
c#htmlasp.net-mvcselectlistselectedvalue

ASP.NET MVC selectlist selectedvalue not working


I am using three different Selectlist to populate three different drop downs. Area, Discipline, and Shift.

The problem I am facing is that it works for the first one but not for the other two. On the form, the selectedvalue works for the area, however for discipline and shift it selects the first one on the list

  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);

The View:

     <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>

Solution

  • So I had mistakenly not deleted the original scaffoldded Viewbags which seemed to cause the problem.

    I deleted them and it all works fine now