ASP.NET MVC 模型下拉绑定给出了奇怪的行为

ASP.NET MVC model dropdown binding gives strange behavior

我有一个 ASP.NET MVC 应用程序,它有资源数据和视图显示多位信息,如名称、年份、部门、组和评论。

我可以绑定所有其他下拉列表,例如部门和组,但不能绑定年份。所选值仅对年份下拉列表没有约束力。我使用了相同的方法来绑定值,但仅年份就很奇怪。

注:model.Year有值

查看:

@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;
}

年份(不具约束力):

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

组和部门(绑定):

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

控制器:

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

我必须通过更改 ViewBag 名称来解决问题。这个问题是因为 Same name for both Model and ViewBag

ModelStateRequestViewData/ViewBag 和最后 Model 的值组成。总之,如果您在 Request 中有一个键,或者对您的问题很重要,一个 ViewBag 成员与您的 属性 同名,那么该值将是HTML Razor 生成。