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!