簡體   English   中英

如何從客戶端驗證中刪除模型的屬性?

[英]How to remove property of model from client side 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>

}

在視圖中,我在用戶編輯中排除了“密碼”字段,並且為了在控制器中驗證模型,我使用了[Bind(Exclude="Password")] ,但是它不起作用,因此我使用了ModelState.Remove("Password"); 但它在服務器端進行驗證。 如果我使用所有字段(包括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; }
}

因此,不是使用“數據”模型,而是使用視圖模型,然后將其在控制器中映射到數據模型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM