Search code examples
c#asp.net-mvcbindingdropdownlistfor

ASP.NET MVC model dropdown binding gives strange behavior


I have an ASP.NET MVC application, which has resource data and view shows multiple bits of information like Name, Year, Department, Group and Comments.

I am able to bind all other dropdown list like Department and Group but not Year. The selected value is not binding for the year dropdown alone. I have used same method for binding the values but year alone giving strange.

Note: model.Year has the value.

View:

@model Application.DAL.NotMappedEntities.AssociateInformation
@{
    var organizationalGroups = (IEnumerable<Application.Core.Models.OrganizationalGroups>)ViewBag.OrganizationalGroups;
    var department = (IEnumerable<Application.Core.Models.Department>)ViewBag.Department;
    var year = (IEnumerable<Application.Core.Models.MPYears>)ViewBag.Years;
}

Year (not binding):

<div class="input-group col-md-2" style="padding-left:15px;">
    @Html.DropDownListFor(model => model.Year, year.ToSelectListItems(x => x.Year, x => x.Id), new { @class = "form-control keep-field-Year track-changes", id = "keep-field-Year", disabled = "disabled" }) 
    @Html.ValidationMessageFor(model => model.Year, "", new { @class = "text-danger" })
    @Html.HiddenFor(model => model.Year, new { id = "YearHidden", @class = "keep-field-Year" })
    <span class="input-group-btn">
        <button class="btn btn-default" type="button" title="Request Change" onclick="sendChangeRequest($(this), 'LHAvailableResource', '@Model.EmployeeId')">
            <em class="fa fa-pencil"></em>
        </button>
    </span>
</div>

Group and Department (binding):

<div class="input-group">
    @Html.DropDownListFor(model => model.DepartmentId, department.ToSelectListItems(x => x.Name, x => x.Id), new { @class = "form-control keep-field-DepartmentId track-changes", id = "keep-field-DepartmentId", disabled = "disabled" })
    @Html.HiddenFor(model => model.DepartmentId, new { @class = "text-danger keep-field-DepartmentId" })
    <span class="input-group-btn">
        <button class="btn btn-default btnDepartmentId" type="button" title="Request Change" onclick="sendChangeRequest($(this), 'LHAvailableResource', '@Model.EmployeeId')">
            <em class="fa fa-pencil"></em>
        </button>
    </span>
</div>

<div class="input-group">
    @Html.DropDownListFor(model => model.DepartmentId, department.ToSelectListItems(x => x.Name, x => x.Id), new { @class = "form-control keep-field-DepartmentId track-changes", id = "keep-field-DepartmentId", disabled = "disabled" })
    @Html.HiddenFor(model => model.DepartmentId, new { @class = "text-danger keep-field-DepartmentId" })
    <span class="input-group-btn">
        <button class="btn btn-default btnDepartmentId" type="button" title="Request Change" onclick="sendChangeRequest($(this), 'LHAvailableResource', '@Model.EmployeeId')">
            <em class="fa fa-pencil"></em>
        </button>
    </span>
</div>

Controller:

this.ViewBag.Department = this._db.GetLHDepartments().Select(s => new Department() { Name = s.Name, Id = s.Id }).DistinctBy(d => d.Id).OrderBy(o => o.Name).ToList();

this.ViewBag.OrganizationalGroups = this._db.GetLHOrganizationalGroups().Select(s => new OrganizationalGroups() { Name = s.GroupName, Id = (int)s.Id }).OrderBy(o => o.Name).ToList();

var YearList = this._db.GetMPYears().Where(s => s.IsActive).Select(s => s.Year).ToList();

ViewBag.MPYears = YearList.Select(s => new MPYears { Id = s, Year = s.ToString() }).OrderBy(o => o.Year).ToList();

Solution

  • I got to fix the problem by changing the ViewBag name. This issue is because of Same name for both Model and ViewBag

    ModelState is composed of values from Request, ViewData/ViewBag and finally Model. Long and short, if you have a key in Request, or important to your issue here, a ViewBag member with the same name as your property, that value will be the set value for the HTML Razor generated.

    Source