简体   繁体   中英

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.

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:

@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"); but it validates on server side. If I am using all the fields (including Password ) it validates on client side. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM