如何从客户端验证中删除模型的属性?

时间:2015-04-30 04:19:19

标签: c# asp.net-mvc unobtrusive-validation

在我的模型中,我有一个类User,其中变量Password是必需属性。

public class User
{
    public int UserId { get; set; }
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }
    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }
    [Required]
    [Display(Name="First Name")]
    public string FirstName { get; set; }
    [Required]
    [Display(Name="Last Name")]
    public string LastName { get; set; }
    public string Address { get; set; }
    [Required]
    [EmailValidator(ErrorMessage = "Email entered is not valid")]
    [Display(Name="Email")]
    public string Email { get; set; }
}

我的EditUser观点:

@model User
@using (Html.BeginForm("EditUser", "Users", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)


    <div class="editor-label">
        @Html.LabelFor(model => model.UserName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.UserName)
        @Html.ValidationMessageFor(model => model.UserName)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.FirstName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.FirstName)
        @Html.ValidationMessageFor(model => model.FirstName)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.LastName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.LastName)
        @Html.ValidationMessageFor(model => model.LastName)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.Address)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Address)
        @Html.ValidationMessageFor(model => model.Address)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.Email)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Email)
        @Html.ValidationMessageFor(model => model.Email)
    </div>
    <p>
        <input type="submit" value="Create User" />
    </p>

}

在视图中,我在用户编辑中排除了字段'password',为了验证控制器中的模型,我使用了[Bind(Exclude="Password")],但它不起作用,所以我使用了{{ 1}}但它在服务器端验证。如果我使用所有字段(包括ModelState.Remove("Password");),它将在客户端验证。 如何在客户端验证这一点(排除一个字段时)?

1 个答案:

答案 0 :(得分:0)

我认为你的模型是数据库中的模型。有时,数据模型不适用于视图。为了使其工作,我建议您介绍解决所有视图方面的不同ViewModel类。很可能它将是您的用户模型,但没有密码字段:

public class UserViewModel
{
    public int UserId { get; set; }

    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [Display(Name="First Name")]
    public string FirstName { get; set; }

    [Required]
    [Display(Name="Last Name")]
    public string LastName { get; set; }

    public string Address { get; set; }

    [Required]
    [EmailValidator(ErrorMessage = "Email entered is not valid")]
    [Display(Name="Email")]
    public string Email { get; set; }
}

因此,您不使用“数据”模型,而是使用视图模型,然后将其在控制器中映射到数据模型。