Home > Backend Development > PHP Tutorial > Share MVC data validation example introduction

Share MVC data validation example introduction

零下一度
Release: 2023-03-10 11:24:02
Original
1007 people have browsed it

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, the data verification of MVC 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; }
  }
Copy after login

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>
}
Copy after login

Effect:

Yes, MVC can be added by adding some

attributes Certain characteristics are used to verify the data. 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 , all correspond to a class in the code Model, we should not modify the model generated through the database, to say the least, even if we add some data verification features to some attributes in this class , then, after the database changes, if I regenerate these Models, the verification features we added before will be gone. 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; }
  }
Copy after login

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; }
  }
Copy after login

However, this will prompt us with 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; }
    }
  }
Copy after login

In this way, our above problems will be easily solved.

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template