集合和枚举前缀相同的MVC 4模型绑定-集合为null

本文关键字:集合 模型 绑定 null 枚举 前缀 MVC | 更新日期: 2023-09-27 18:22:17

我的MVC 4项目(它是sitefinity web应用程序的一部分)正确绑定模型时遇到了问题。在我将MembershipType枚举引入视图之前,一切都很好。枚举绑定正确,但Members集合始终为null。枚举的存在似乎就触发了这个问题。使用文本框、下拉列表还是隐藏输入都无关紧要。注释掉枚举后,集合将再次成功绑定。当Members集合为null时,模型状态是有效的,没有错误,只是没有成员属性的条目。所有表单值都已发布,并且格式似乎正确。

有人知道为什么绑定集合可能会失败,或者如何通过绑定吗?是否有在模型中过帐集合的替代方法?

以下是一些代码详细信息:

 public class Membership
    {
        public Guid Id { get; set; }
        public string UrlName { get; set; }
        public string MembershipNumber { get; set; }
        public IList<Member> Members { get; set; }
        public string Company { get; set; }
        public string PostalAddressee { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string PostCode { get; set; }
        public string Country { get; set; }
        public MembershipType MembershipType { get; set; }
        public DateTime ExpiryDate { get; set; }
    }

 public enum MembershipType{
        Single,
        Bonsai,
        ConcessionX2,
        Life,
        Donation,
        ExMember,
        Deceased,
        Association,
        Corporate,
        Concession,
        NotFinancial,
        Household,
        NoCost,
        NonMember
    }
    public class Member
    {
        public Guid Id { get; set; }
        public Guid MembershipId { get; set; }
        public string UrlName { get; set; }
        public string Title { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public bool OptOutOfEmail { get; set; }
}

控制器:

 [HttpPost]
        public ActionResult Detail(Membership membership)
        {
            ...
        }

视图:

@using (Html.BeginFormSitefinity("Detail", "Membership", FormMethod.Post, new { Id = "editMemberForm", role = "form" }))
{
    foreach (var member in Model.Members)
    {
        int i = 0;
        <h1>@member.Title @member.FirstName @member.LastName</h1>
        <label>
            Title:
            @Html.TextBox("Members[" + i.ToString() + "].Title", @Model.Members[i].Title)
        </label>
        <label>
            First Name:
            @Html.TextBox("Members[" + i.ToString() + "].FirstName", @Model.Members[i].FirstName)
        </label>
        <label>
            Last Name:
            @Html.TextBox("Members[" + i.ToString() + "].LastName", @Model.Members[i].LastName)
        </label>
        <label>
            Email:
            @Html.TextBox("Members[" + i.ToString() + "].Email", @Model.Members[i].Email)
        </label>
        <label>
            Opt out of Email:
            @Html.CheckBox("Members[" + i.ToString() + "].OptOutOfEmail", @Model.Members[i].OptOutOfEmail)
        </label>
        <label>
            Founding Member:
            @Html.CheckBox("Members[" + i.ToString() + "].FoundingMember", @Model.Members[i].FoundingMember)
        </label>
        <label>
            Council Member:
            @Html.CheckBox("Members[" + i.ToString() + "].CouncilMember", @Model.Members[i].CouncilMember)
        </label>
        <label>
            Trained Arboretum Guide:
            @Html.CheckBox("Members[" + i.ToString() + "].TrainedArboretumGuide", @Model.Members[i].TrainedArboretumGuide)
        </label>
        <label>
            Bonsai Guide:
            @Html.CheckBox("Members[" + i.ToString() + "].BonsaiGuide", @Model.Members[i].BonsaiGuide)
        </label>
        <label>
            Vulnerable People Number:
            @Html.TextBox("Members[" + i.ToString() + "].VulnerablePeopleNumber", @Model.Members[i].VulnerablePeopleNumber)
        </label>
        <label>
            VPN Expiry Date:
            @Html.TextBox("Members[" + i.ToString() + "].VulnerablePeopleExpiry", (Model.Members[i].VulnerablePeopleExpiry == null) ? "" : ((DateTime)Model.Members[i].VulnerablePeopleExpiry).ToString("dd/MMM/yyyy"), new { @class = "vpnExp" })
        </label>
        @Html.Hidden("Members[" + i.ToString() + "].Id", @Model.Members[i].Id)
    }
    <label>
        Expiry Date:
        @Html.TextBoxFor(m => m.ExpiryDate, new { Value = Model.ExpiryDate.ToString("dd/MMM/yyyy") })
    </label>
    <label>
        Membership Number:
        @Html.TextBoxFor(m => m.MembershipNumber)
    </label>
    @*<label>
        Membership Type:
        @Html.DropDownList("MembershipType", new SelectList(Enum.GetValues(typeof(SitefinityWebApp.MembershipType)).Cast<SitefinityWebApp.MembershipType>()))
    </label>*@
    @*@Html.HiddenFor(m => m.MembershipType);*@
    <input type="submit" />

集合和枚举前缀相同的MVC 4模型绑定-集合为null

所以这实际上是asp.net MVC 4中DefaultModelBinder中的一个错误。将找不到名称是同一类中字段名称前缀的集合。更多详细信息请点击此处:http://rarelyupdated.azurewebsites.net/posts/5606734

我更改了

IList<Member> Members

IList<Member> People

一切都很好。