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

C# 如何从客户端验证中删除模型的属性?,c#,asp.net-mvc,unobtrusive-validation,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 =

在我的模型中,我有一个类
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>

}
@模型用户
@使用(Html.BeginForm(“EditUser”、“Users”、FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@LabelFor(model=>model.UserName)
@EditorFor(model=>model.UserName)
@Html.ValidationMessageFor(model=>model.UserName)
@LabelFor(model=>model.FirstName)
@EditorFor(model=>model.FirstName)
@Html.ValidationMessageFor(model=>model.FirstName)
@LabelFor(model=>model.LastName)
@EditorFor(model=>model.LastName)
@Html.ValidationMessageFor(model=>model.LastName)
@LabelFor(model=>model.Address)
@EditorFor(model=>model.Address)
@Html.ValidationMessageFor(model=>model.Address)
@LabelFor(model=>model.Email)
@EditorFor(model=>model.Email)
@Html.ValidationMessageFor(model=>model.Email)

}
在视图中,我在用户编辑中排除了字段“password”,为了验证控制器中的模型,我使用了
[Bind(Exclude=“password”)]
,但它不起作用,因此我使用了
ModelState.Remove(“password”)但它在服务器端进行验证。如果我正在使用所有字段(包括
密码
),它将在客户端进行验证。
如何在客户端(排除一个字段时)对此进行验证?

我假定您的模型是数据库中的模型。有时数据模型不适用于视图。为了使其正常工作,我建议您引入不同的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; }
}

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

您确定在您的页面中包含了
jquery.validate.js
和`jquery.validate.unobtrusive.js``吗?我读了两遍您的问题,但仍然不明白。请将其重新格式化为多个句子好吗?如果您没有
password
的控件,那么如何在客户端验证它(它不存在)。您想在问题中说清楚什么。。。您对密码没有控制权,也不是必需的,那么为什么您要保留在视图中。@StephenMuecke我有一个“创建用户”页面,用户可以在创建新用户时输入密码。所以我在用户类中使用了“password”字段。客户端验证意味着在不回发的情况下验证字段