Home  >  Article  >  Backend Development  >  Detailed explanation of data validation for MVC

Detailed explanation of data validation for MVC

巴扎黑
巴扎黑Original
2017-08-08 13:36:511984browse

This article mainly introduces the relevant information of MVC data verification in detail, which has certain reference value. Interested friends can refer to it

1. General situation

For those who have used the MVC framework, MVC data verification will be familiar. For example, I have a Model as follows:


##

public class UserInfo
  {
    [Required(ErrorMessage = "UserName不可为空1111")]
    public string UserName { get; set; }
    public string Sex { get; set; }
    public string Mobile { get; set; }
    public string Address { get; set; }
  }

Front end :


@using (Html.BeginForm()) 
{
  @Html.AntiForgeryToken()
  <p class="form-horizontal">
    <h4>UserInfo</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <p class="form-group">
      @Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })
      <p class="col-md-10">
        @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
      </p>
    </p>
    <p class="form-group">
      @Html.LabelFor(model => model.Sex, htmlAttributes: new { @class = "control-label col-md-2" })
      <p class="col-md-10">
        @Html.EditorFor(model => model.Sex, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.Sex, "", new { @class = "text-danger" })
      </p>
    </p>
    <p class="form-group">
      @Html.LabelFor(model => model.Mobile, htmlAttributes: new { @class = "control-label col-md-2" })
      <p class="col-md-10">
        @Html.EditorFor(model => model.Mobile, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.Mobile, "", new { @class = "text-danger" })
      </p>
    </p>
    <p class="form-group">
      @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
      <p class="col-md-10">
        @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
      </p>
    </p>
    <p class="form-group">
      <p class="col-md-offset-2 col-md-10">
        <input type="submit" value="Create" class="btn btn-default" />
      </p>
    </p>
  </p>
}

Effect:

Yes, MVC can modify the data by adding certain characteristics to some attributes. authenticating. This may not be unfamiliar to everyone.

If that’s all it is, then there’s nothing wrong with it.

2. Common situations

In actual development, we mostly use EF or other methods to make every table or view in the database. The corresponding class model in the code should not be modified for the model generated through the database. To take a step back, even if we add some data verification features to some attributes in this class, then after the database changes, if I re- When generating these Models, the verification features we added before will no longer exist. So, how do we solve this problem?

If:


public class UserInfo
  {  
    public string UserName { get; set; }
    public string Sex { get; set; }
    public string Mobile { get; set; }
    public string Address { get; set; }
  }

UserInfo is a model generated through the database. We should not modify the model generated by the database. But that is, we need to perform data verification on certain attributes in this model. For example, we need to perform non-null verification on the UserName attribute. So how do we do it?

Everyone usually thinks of partial classification. Yes, we can solve the above problems through partial classification.

First, we add the keyword partial to the class in the model, and then we write a partial class of this model.


public partial class UserInfo
  {
    [Required(ErrorMessage = "UserName不可为空1111")]
    public string UserName { get; set; }
  }

However, this will prompt us an error, that is, there are duplicate attributes in the class. Yes, in some classes, attributes cannot have the same name. So, what should we do? The MVC framework has already given us a solution.

We can write like this:


[MetadataType(typeof(MeteUserInfo))]
  public partial class UserInfo
  {
    private class MeteUserInfo
    {
      [Required(ErrorMessage = "UserName不可为空1111")]
      public string UserName { get; set; }
    }
  }

In this way, our above problems will be easily solved

The above is the detailed content of Detailed explanation of data validation for MVC. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn