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; } }
@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> }
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; } }
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 Detailed explanation of data validation for MVC. For more information, please follow other related articles on the PHP Chinese website!