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; } }
@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> }
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 orview 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; } }
public partial class UserInfo { [Required(ErrorMessage = "UserName不可为空1111")] public string UserName { get; set; } }
[MetadataType(typeof(MeteUserInfo))] public partial class UserInfo { private class MeteUserInfo { [Required(ErrorMessage = "UserName不可为空1111")] public string UserName { get; set; } } }
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!