0

我在不需要的属性上遇到了 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 中也没有。那么为什么这两个是必需的?它是一个谜。

4

1 回答 1

0

您不应该在文本框中同时使用disabledandreadonly属性:

@Html.TextBoxFor(m => m.Id, new { disabled = "disabled", @readonly = "readonly" })

我猜禁用属性优先,并且当您提交表单时,Id 的值永远不会发送到服务器。这就是您收到模型状态错误的原因。因为您的 id 属性是必需的。现在你可能会告诉我它没有用[Required]属性装饰,但这没关系。由于您已将其声明为不可为空的整数,因此它是隐式必需的,并且框架会自动将其设为必需。如果您不希望发生这种情况,则应将其声明为可为空的整数。

所以回到你的观点,如果你只想显示 id,不允许用户修改它,使用 readonly:

@Html.TextBoxFor(m => m.Id, new { @readonly = "readonly" })

显然不要认为如果您将文本框设为只读,则用户无法修改它。普通用户不会。但是黑客总是可以在这个文本框中输入他想要的任何值并伪造请求。所以绝对不要把它当作某种安全或其他东西。

于 2012-07-15T17:55:03.460 回答