简体   繁体   English

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

[英]How to remove property of model from client side validation?

In my model I have a class User where the variable Password is a required property. 在我的模型中,我有一个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; }
}

My EditUser view: 我的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>

}

In the view I have excluded the field 'password' in user edit, and in order to validate the model in the controller I have used [Bind(Exclude="Password")] , but it doesn't work, so I have used ModelState.Remove("Password"); 在视图中,我在用户编辑中排除了“密码”字段,并且为了在控制器中验证模型,我使用了[Bind(Exclude="Password")] ,但是它不起作用,因此我使用了ModelState.Remove("Password"); but it validates on server side. 但它在服务器端进行验证。 If I am using all the fields (including Password ) it validates on client side. 如果我使用所有字段(包括Password ),它将在客户端进行验证。 How can I validate this on the client side (when excluding one field)? 如何在客户端(排除一个字段时)进行验证?

I presume your model is model from the database. 我假设您的模型是数据库中的模型。 Sometimes data models do not work for views. 有时,数据模型不适用于视图。 To make it work I recommend you to introduce different ViewModel class that addresses all the view aspects. 为了使其正常工作,我建议您引入解决所有视图方面的不同ViewModel类。 Most likely it will be your User model but without password field: 最有可能的是您的用户模型,但没有密码字段:

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; }
}

So instead of using your "data" model, you use view model and then map it in controller to a data model. 因此,不是使用“数据”模型,而是使用视图模型,然后将其在控制器中映射到数据模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在日期字符串模型属性上获得客户端验证 - How to get client side validation on a date string model property 是否可以在MVC中禁用单个模型属性的客户端验证 - Is it possible to disable client side validation for a single model property in MVC 如何在视图 Model 中使用 Html 验证从客户端验证? - How to use Html Validation from the client side without validation in View Model? 如何根据客户端验证错误添加和删除 CSS 类? - How to add and remove CSS classes based on validation errors on client side? 没有模型类的htmlhelper中的客户端验证 - client side validation in htmlhelper without Model Class MVC 在 model 字段上禁用客户端验证 - MVC disabling client side validation on a model field 如何同时显示来自服务器端流利验证和客户端验证的错误消息? - How to display error messages from server side fluent validation AND client side validation at the same time? 如何显示带有客户端验证的验证摘要? - How to display Validation Summary with Client side validation? 如何为有条件的必需属性实现客户端验证 - Razor ASP.NET 核心 - How to implement client side validation for a conditional required property - Razor ASP.NET core 打包/多模型视图的客户端验证 - Client-side validation for wrapped/multi model views
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM