Share MVC data verification example-php Tutorial

Source: Internet
Author: User
This article mainly introduces the MVC data verification materials in detail, which has some reference value, interested friends can refer to this article for details about MVC data verification, which has some reference value. interested friends can refer

I. general situation

For those who have used the MVC framework, it is no stranger to MVC data verification. for example, I have a Model as follows:


Public class UserInfo {[Required (ErrorMessage = "UserName cannot be blank 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()  

UserInfo

@Html.ValidationSummary(true, "", new { @class = "text-danger" })

@Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })

@Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })

@Html.LabelFor(model => model.Sex, htmlAttributes: new { @class = "control-label col-md-2" })

@Html.EditorFor(model => model.Sex, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Sex, "", new { @class = "text-danger" })

@Html.LabelFor(model => model.Mobile, htmlAttributes: new { @class = "control-label col-md-2" })

@Html.EditorFor(model => model.Mobile, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Mobile, "", new { @class = "text-danger" })

@Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })

@Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })

}

Effect:

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 by the database. we cannot modify the model generated by the database. But that is, we need to perform data verification on some attributes in this model. for example, we need to perform non-empty verification on the UserName attribute. what should we do?

We usually think 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 write a partial classification for the model.


Public partial class UserInfo {[Required (ErrorMessage = "UserName cannot be blank 1111")] public string UserName {get; set ;}}

However, this will prompt us an error, that is, duplicate attributes exist in the class. Yes, attributes cannot be renamed in the partial classification. So what should we do? the MVC framework has already provided us a solution.

We can write as follows:


[MetadataType (typeof (MeteUserInfo)] public partial class UserInfo {private class MeteUserInfo {[Required (ErrorMessage = "UserName cannot be blank 1111")] public string UserName {get; set ;}}}

In this way, the above problems will be solved.

The above is a detailed description of the MVC data verification instance. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.