我的MVC 4项目(它是sitefinity网络应用程序的一部分)遇到问题正确地绑定了模型。一切正常,直到我将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" />
答案 0 :(得分:0)
因此,这实际上是asp.net MVC 4中DefaultModelBinder中的一个错误。将找不到一个名称是同一个类中字段名称前缀的集合。更多细节在这里: http://rarelyupdated.azurewebsites.net/posts/5606734
我改变了我的
IList<Member> Members
到
IList<Member> People
一切都很顺利。