我在不需要的属性上遇到了 Model.IsValid 问题。
这是代码。
Edit.cshtml 文件中的 BeginForm
@using (Html.BeginForm("Edit", "Member", FormMethod.Post, new { enctype = "multipart/formdata" }))
{
@Html.Partial("_MemberForm", Model.Member)
}
MemberEditViewModel:(用于 Edit.cshtml 文件)
public class MemberEditViewModel
{
public MemberFormModel Member { get; set; }
}
MemberFormModel:
public class MemberFormModel : ICreateMemberCommand, IValidatableObject
{
public int Id { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
public string SocialSecurityNumber { get; set; }
[Required]
public int PinCode { get; set; }
[Required]
public char Gender { get; set; }
public string Email { get; set; }
[Required]
public string Address { get; set; }
[Required]
public string ZipCode { get; set; }
[Required]
public string ZipAddress { get; set; }
public string Phone { get; set; }
public string Phone2 { get; set; }
[Required]
public string City { get; set; }
[Required]
public int CountryId { get; set; }
//not required (but still displaying error it's required)
public Membership Membership { get; set; }
// not required (displaying error it's required)
public PunchCard PunchCard { get; set; }
public bool IsActive { get; set; }
_MemberForm.cshtml 块(部分)
<fieldset>
<dl>
<dt>@Html.LabelFor(m => m.Id)</dt>
<dd>@Html.TextBoxFor(m => m.Id, new { disabled = "disabled", @readonly = "readonly" })</dd>
<dt>@Html.LabelFor(m => m.PinCode)</dt>
<dd>@Html.EditorFor(m => m.PinCode)</dd>
<!-- problem with membership, maybe with the .FromData/ToDate ? -->
<dt>@Html.LabelFor(m => m.Membership)</dt>
<dd>@Html.EditorFor(m => m.Membership.FromDate, new { @name = "Membership" }) -
@Html.EditorFor(m => m.Membership.ToDate, new { @name="Membership"})</dd>
<!-- problem with punch card, maybe with the .Times ? -->
<dt>@Html.LabelFor(m => m.PunchCard)</dt>
<dd>@Html.EditorFor(m => m.PunchCard.Times, new { @name = "PunchCard" })</dd>
</dl>
</fieldset>
MemberController 编辑动作
// POST: /Members/10002/Edit
[HttpPost]
public ActionResult Edit(FormCollection formValues, MemberFormModel memberForm)
{
var errors = ModelState.Values.SelectMany(v => v.Errors);
if(IsSaveOperation(formValues)){
if(TryUpdateMember(memberForm)){
return RedirectToAction("Details", "Member", new {id = memberForm.Id});
}
}
var mm = new MemberEditViewModel{ Member = memberForm };
return View(mm);
}
会员资格.cs
public class Membership
{
public Membership(){ /* empty constructor */}
public Membership(int id, int memberId, DateTime fromDate, DateTime toDate)
{
Id = id;
MemberId = memberId;
FromDate = fromDate;
ToDate = toDate;
}
public int Id { get; set; }
public int MemberId { get; set; }
[DataType(DataType.Date)]
public DateTime FromDate { get; set; }
[DataType(DataType.Date)]
public DateTime ToDate { get; set; }
}
打卡卡.cs
public class PunchCard
{
public PunchCard() { /* empty constructor */ }
public PunchCard(int memberId, int times, DateTime createdDate, DateTime modifiedDate)
{
this.MemberId = memberId;
this.Times = times;
this.CreatedDate = createdDate;
this.ModifiedDate = modifiedDate;
}
public int Id { get; set; }
public int MemberId { get; set; }
public int Times { get; set; }
public DateTime CreatedDate { get; set; }
public DateTime ModifiedDate { get; set; }
}
你看我没有任何 [Required] 属性,在 MemberFormModel 中也没有。那么为什么这两个是必需的?它是一个谜。