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

Example Analysis of client Verification of ASP.NET MVC5

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

Share

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

This article mainly shows you the "sample analysis of client-side verification of ASP.NET MVC5", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let the editor lead you to study and study the "sample analysis of client-side verification of ASP.NET MVC5".

Client-side verification, using Jquery and Jquery plug-ins to implement [jquery.validate.min.js and jquery.validate.unobtrusive.min.js)]

In server-side verification, the page must be submitted to the server for verification. If the data verification fails, the server will send a response to the client, and then the client will process it according to the corresponding information. Client verification is different. As soon as the data entered by the user is submitted, the client will verify it first. If it does not pass, the client will report an error and will not submit it to the server for verification. If it is passed, the request will be transmitted to the server.

If you are using a version above VS2012, it comes with client-side authentication enabled: [of course, you can also manually add the following configuration. ]

We are still the previous project:

Using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations;using System.Linq;using System.Web;namespace Server_Side_Validation_IN_MVC.Models {public class StudentServer {[Required (ErrorMessage= "Name is required")] public string Name {get; set;} [Required (ErrorMessage= "email must")] [EmailAddress (ErrorMessage= "email format is incorrect")] public string Email {get; set } public ActionResult SeverSideIndex () {return View ();} [HttpPost] public ActionResult SeverSideIndex (StudentServer model) {if (ModelState.IsValid) {ViewBag.Name = model.Name; ViewBag.Email = model.Email;} return View ();}

The difference is, here, I add views, different:

Note that the reference script library must be checked here, that is, the Jquery and Jquery plug-ins are introduced in order to perform client-side verification:

Modify the default route:

Public static void RegisterRoutes (RouteCollection routes) {routes.IgnoreRoute ("{resource} .axd / {* pathInfo}"); routes.MapRoute (name: "Default", url: "{controller} / {action} / {id}", defaults: new {controller = "Student", action = "SeverSideIndex", id = UrlParameter.Optional});}

Run the project:

We add a breakpoint to the controller's POST method

Click the button directly: the POST method is not called, and the validity of the data is verified directly on the client side.

Enter legal data:

The breakpoint is triggered, that is, after the client verification is passed, it is submitted to the server for further processing.

Well, this is client-side authentication, which is relatively simple. But be careful to introduce the Jquery plug-in

The above is all the contents of the article "sample Analysis of client-side validation of ASP.NET MVC5". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more 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