Share MVC data validation example introduction
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Creating a drop-down list in an Excel worksheet is easy, as long as it's a regular drop-down menu. But what if you have to make it special by adding a special symbol, or make it even more special by adding some text as well as symbols? Ok, sounds interesting but wondering if this is possible? What's an answer you don't know when Geek Page is here to help? This article is all about creating dropdown menus with symbols as well as symbols and text. Hope you enjoyed reading this article! Also Read: How to Add Dropdown Menu in Microsoft Excel Part 1: Create a Dropdown List with Only Symbols To create a dropdown menu with symbols, we first need to create the source

Introduction In today's rapidly evolving digital world, it is crucial to build robust, flexible and maintainable WEB applications. The PHPmvc architecture provides an ideal solution to achieve this goal. MVC (Model-View-Controller) is a widely used design pattern that separates various aspects of an application into independent components. The foundation of MVC architecture The core principle of MVC architecture is separation of concerns: Model: encapsulates the data and business logic of the application. View: Responsible for presenting data and handling user interaction. Controller: Coordinates the interaction between models and views, manages user requests and business logic. PHPMVC Architecture The phpMVC architecture follows the traditional MVC pattern, but also introduces language-specific features. The following is PHPMVC

Example of new features in PHP8: How to use type declarations and code to strengthen data validation? Introduction: With the release of PHP8, developers have welcomed a series of new features and improvements. One of the most exciting is the ability for type declarations and code to enforce data validation. This article will take some practical examples to introduce how to use these new features to strengthen data validation and improve code readability and maintainability. Advantages of type declaration: Before PHP7, the type of variables could be changed at will, which brought great difficulties to data verification.

The MVC architecture (Model-View-Controller) is one of the most popular patterns in PHP development because it provides a clear structure for organizing code and simplifying the development of WEB applications. While basic MVC principles are sufficient for most web applications, it has some limitations for applications that need to handle complex data or implement advanced functionality. Separating the model layer Separating the model layer is a common technique in advanced MVC architecture. It involves breaking down a model class into smaller subclasses, each focusing on a specific functionality. For example, for an e-commerce application, you might break down the main model class into an order model, a product model, and a customer model. This separation helps improve code maintainability and reusability. Use dependency injection

Do you know how to use excel data verification? Below, the editor will bring you how to use excel data verification. I hope it will be helpful to everyone. Let’s learn with the editor! 1. First, in the EXCEL table, select the required Set the cell for the drop-down option, as shown in the figure below: 2. Then click [Data] on the menu bar, as shown in the figure below: 3. After opening the data menu, you will see the [Data Validation] option, click [Data] After verification], continue to click [Data Verification] in the open options to open the data verification window for settings, as shown in the figure below: The above is the entire content of how to use excel data verification brought by the editor. I hope it will be helpful to you. Everyone can help.

How to perform data reliability verification and model evaluation in Python Data reliability verification and model evaluation are very important steps when working with machine learning and data science models. This article will introduce how to use Python for data reliability verification and model evaluation, and provide specific code examples. Data Reliability Validation Data reliability validation refers to the verification of the data used to determine its quality and reliability. The following are some commonly used data available

With the rapid development of the Internet, developers' tasks have also become diverse and complex. Especially for PHP language developers, one of the most common problems faced during the development process is the error of data inconsistency between the development environment and the production environment. Therefore, how to handle these errors is an important issue that developers must face when developing PHP applications. The difference between development environment and production environment The first thing to make clear is that development environment and production environment are different, and they have different settings and configurations. in development environment

ThinkPHP6 form validation and data validation: ensuring the legality of data. In the process of web application development, form validation is an important part of ensuring the legality and integrity of data. The ThinkPHP6 framework provides powerful form validation and data validation functions, which can simplify the development process and help us reduce the occurrence of errors and vulnerabilities. 1. Form validation validation rule declaration ThinkPHP6 supports the use of annotations to declare validation rules for the controller's request method. We can do this on the request method of the controller
