Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to validate MVC data

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to verify the data of MVC. It is very detailed and has a certain reference value. Friends who are interested must finish reading it.

I. General circumstances

For people who have used the MVC framework, it is no stranger to MVC data validation. For example, I have a Model like this:

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

Yes, MVC can validate data by adding properties to some attributes. This may be no stranger to everyone.

If that's all it is, then it doesn't make any sense.

II. Common situations

In actual development, we mostly use EF or other ways to make every table or view in the database correspond to a class model in the code. It is not appropriate for us to modify the model generated through the database. To say the least, even if we add some data validation features to some attributes in this class, then, after the database changes, if I regenerate these Model The validation features we added before will be gone, so how can we solve such a 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;}}

UserInfo is a model generated by the database, and we should not modify the model generated by the database. But that is, we also need to validate some of the attributes in this model, such as non-empty validation of the UserName attribute, so how do we do that?

People usually think of some classes, yes, we can solve the above problems through some classes.

First, we add the keyword partial to the class in the model, and then we write a partial class of the model.

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

However, this will remind us of an error, that is, there are duplicate attributes in the class, yes, in some classes, attributes cannot be renamed. So, what are we going to do? the MVC framework has given us a solution.

We can write like this:

[MetadataType (typeof (MeteUserInfo))] public partial class UserInfo {private class MeteUserInfo {[Required (ErrorMessage = "UserName cannot be empty 1111")] public string UserName {get; set;}} above is all the content of the article "how to validate MVC data". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report