必須ではないプロパティの 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; }
}
メンバーフォームモデル:
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; }
}
PunchCard.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; }
}
MemberFormModel にも [Required] 属性がありません。では、なぜこれらの 2 つが Required なのですか? それは謎です。