SlideShare a Scribd company logo
MVC 3-RAZOR-Validations

Krunal Trivedi
Corporate Trainer DotNet,Silverlight,SPS 2010, SharePoint 15,MVC , Windows
Phone , Windows 8
MCT S For .NET Web Technology
MCTS For SharePoint Server 2010 Development
Co-Founder at Aavid Technologies
Email:krunaltrivedi@live.in
Contact : 09998472789
MVC 3 Web Applicaiton
MVC 3-RAZOR Validation
ModelAdd New ClassCustomerClass.cs
ControllerAdd New ControllerCustomerController-->Empty Controller
Right Click on Index Action-Create View
Strongly-Typed View
Modify CustomerClass as shown below…
We are going to implement Validaiton
Create one directoryUtilityAdd New ClassAgeValidation.cs
Prepare Class logic…that is going to make sure age greater than 21
Open CustomerController
Create New ActionDisplayCustomer
Right Click on DisplayCustomerAdd ViewStrongly-Typed View
Run the application.
We are not getting proper validation messages…
Modify Your Index.cshtml view as shown below…
Now , Run Application.
We get proper validation messages.
MVC 3-RAZOR Validation
MVC 3-RAZOR Validation
• Direct Approach to Validation is..
  – You can Directly right down validation logic inside
    Controller Action…
  – Let’s try…
Modify CustomerClass.cs as shown below….Comment out
validations attributes for FirstName and LastName
Modify DisplayCustomer Action of CustomerController
Run Application…
For FirstName and LastName we are getting validation messages from
DisplayCustomer Action.
We will use @Html.ValidationSummary(true)
It is used when there is some problem arising from an interaction between two or
more property values.
Modify Display Customer Action as shown below.
Run Application.
• In this case , it is not possible to display Property-Level
  validation messages.
• Suppose , we want to display both Property-Level validation
  messages and model-level validation messages.
• Let’s try…
Modify your Index.cshtml as shown below…
Run Application…
Modify output..and check it again…
MVC 3-RAZOR Validation
Client-Side Validation
• The MVC Framework supports unobtrusive client-side
  validation.

• The term unobtrusive means that validation rules are
  expressed using attributes added to the HTML elements that
  we generate.
• These are interpreted by a JavaScript library that is included
  as part of the MVC Framework .
Unobstrusive means….3 things
• The first is that the JavaScript that performs the validation
 is kept separate from the HTML elements, which means we
don’t have to include client-side validation logic into our views
and that the HTML we generate is easier to read.
• The second characteristic is that the validation is performed
  using progressive enhancement.
• This means that if a user’s browser doesn’t support all of the
  JavaScript features we require for client-side validation, then
  the validation will be performed using simpler techniques.
• For example, if the user has disabled JavaScript, then server-
  side validation will be seamlessly performed without the user
  being otherwise penalized (no unpleasant error messages or
  special steps to take).
• The third characteristic is a set of best practices to mitigate
  the effect of browser inconsistencies and behaviors.
Enabling-Disabling Client-Side Validation

Open web.config and modify appsetting as shown below.
Referencing the JavaScript Libraries Required for Client-Side Validation
Open _Layout.cshtml and observer the libraries added
From Scriptsadd following .js file to _Layout.cshtml
• That’s it…
• Run the application…
• It works Right???
The order in which the jQuery files are referenced is significant.
             If you change the order, you will find
          that the client validation is not performed.
• The scripts folder contains two versions of each JavaScript
  library.
• The versions whose name ends with min.js are the minimized
  versions, meaning that all of the whitespace, comments, and
  other nonessential content have been removed to reduce the
  size of the library file.
• The minimized files can be much smaller and are typically
  used in production to reduce the amount of data that the
  client downloads.
• During development, the unminimized versions are typically
  used so that the JavaScript can be debugged (or just read) if
  problems arise.
We required…one more library..that is really important…
Run application…
Observer the client-side validation.
Client and Server side Validation messages.
MVC 3-RAZOR Validation
How it works !!!




• One of the benefits of using the MVC Framework client-side
  validation feature is that we don’t have to write any
  JavaScript. Instead, the validation rules are expressed using
  HTML attributes. Here is the
• HTML that is rendered by the Html.EditorFor helper for the
  ClientName property when client-side
• validation is disabled:
Disable the Client-Side Validation as shown below….
Run Application.
Generate some validation message.
MVC 3-RAZOR Validation
MVC 3-RAZOR Validation
Enable Client-side validation..
By default it is enabled..so need to write following line..
If you write…..Cool…no issues…
Now generate the validation and check the page source.
• The MVC client-side validation support doesn’t generate any
  JavaScript or JSON data to direct the validation process; like
  much of the rest of the MVC Framework, we rely on
  convention.
• The first attribute that was added is data-val. The jQuery
  Validation library identifies those fields that require validation
  by looking for this attribute.
• Individual validation rules are specified using an attribute in
  the form data-val-<name>, where name is the rule to be
  applied. So, for example, the Required attribute
• The interpretation of the required and length validation rules
  is provided by the jQuery Validation library, on which the MVC
  client validation features are built.
• The MVC client-validation features are built on top of the
  jQuery Validation library, and if you prefer, you can use the
  Validation library directly and ignore the MVC features.
Direct Jquery Validation.

More Related Content

PPTX
Web api 2 With MVC 5 With TrainerKrunal
PPT
Wcf routing kt
PPT
ASP.NET Session 10
PPSX
Ajax part i
PPT
Introduction To Asp.Net Ajax
PPTX
Ajax control asp.net
PPTX
Ajax and ASP.NET AJAX
Web api 2 With MVC 5 With TrainerKrunal
Wcf routing kt
ASP.NET Session 10
Ajax part i
Introduction To Asp.Net Ajax
Ajax control asp.net
Ajax and ASP.NET AJAX

What's hot (20)

PPTX
Cache control directive
PPT
Asp.Net Ajax Component Development
PPT
SOAP-UI The Web service Testing
PPTX
Introduction to asp
PPTX
Ch3 server controls
PPT
2310 b 06
PPTX
MVC Training Part 2
PPTX
Web Services Testing
PPTX
Ajax control tool kit
PPT
Server Controls of ASP.Net
PPT
Web controls
PPT
Ajax Highlights
PPTX
Controls
PPT
ASP.NET 03 - Working With Web Server Controls
PPT
2310 b 17
PPTX
ASP .NET MVC
PPTX
Web services testing
PPT
Webservices testing using SoapUI
Cache control directive
Asp.Net Ajax Component Development
SOAP-UI The Web service Testing
Introduction to asp
Ch3 server controls
2310 b 06
MVC Training Part 2
Web Services Testing
Ajax control tool kit
Server Controls of ASP.Net
Web controls
Ajax Highlights
Controls
ASP.NET 03 - Working With Web Server Controls
2310 b 17
ASP .NET MVC
Web services testing
Webservices testing using SoapUI
Ad

Viewers also liked (20)

PPT
On the incarnation
PDF
Institutional Presentation 1Q16
PPT
Slideshare
PPTX
Frases
PDF
How to Create Multilingual Websites
PDF
Transforming kyoto
PPT
Como o Design de I
PPTX
The of Social Media - Women in Management
PPTX
Social media 101 kimberly high
PDF
TIM FIber Day Presentation
PDF
Results presentation 2 q14 eng
PDF
Cognitive Enterprise Services
PPT
Yet anothr test of slide share
PDF
Jihad Internet - Ecommerce
PDF
Meeting with investors of march 2013
PDF
Tim meeting with investors - sep 2012
PPT
August 10th, 2009 Dan Day ifbyphone
PDF
Press Release 1 Q03 Tele Celular Sul En
PPT
ICWL 2009
PDF
Edisi19mei nas
On the incarnation
Institutional Presentation 1Q16
Slideshare
Frases
How to Create Multilingual Websites
Transforming kyoto
Como o Design de I
The of Social Media - Women in Management
Social media 101 kimberly high
TIM FIber Day Presentation
Results presentation 2 q14 eng
Cognitive Enterprise Services
Yet anothr test of slide share
Jihad Internet - Ecommerce
Meeting with investors of march 2013
Tim meeting with investors - sep 2012
August 10th, 2009 Dan Day ifbyphone
Press Release 1 Q03 Tele Celular Sul En
ICWL 2009
Edisi19mei nas
Ad

Similar to MVC 3-RAZOR Validation (20)

PPTX
Mvc part 1
PPTX
Client control
PPTX
Model view controller (mvc)
DOCX
JavaScript
PPTX
Valentine with Angular js - Introduction
PPT
Mvc architecture
PPTX
Introduction of ASP.NET MVC and AngularJS
PPTX
Automated Acceptance Testing Example
PDF
Angular - Chapter 3 - Components
PPTX
MVC Framework
PDF
Guide To Using Inspect Element on Mac.pdf
PPT
Mvc 130330091359-phpapp01
PPTX
Spring Web Presentation 123143242341234234
PPT
CTTDNUG ASP.NET MVC
PPTX
Chapter5-Bypass-ClientSide-Control-Presentation.pptx
PDF
Introduction to Angular Js
PDF
Introduction to Angularjs : kishan kumar
PPTX
Introduction to Angularjs
PDF
Spring Framework-II
Mvc part 1
Client control
Model view controller (mvc)
JavaScript
Valentine with Angular js - Introduction
Mvc architecture
Introduction of ASP.NET MVC and AngularJS
Automated Acceptance Testing Example
Angular - Chapter 3 - Components
MVC Framework
Guide To Using Inspect Element on Mac.pdf
Mvc 130330091359-phpapp01
Spring Web Presentation 123143242341234234
CTTDNUG ASP.NET MVC
Chapter5-Bypass-ClientSide-Control-Presentation.pptx
Introduction to Angular Js
Introduction to Angularjs : kishan kumar
Introduction to Angularjs
Spring Framework-II

More from Krunal Trivedi (9)

PPTX
Certifications for Azure Developers
PPTX
Azure Functions - Serverless Computing
PPTX
Azure App Service for Windows Container
PPTX
Chat application with Azure SignalR Service
PPTX
Implementing enterprise cloud scenarios with Microsoft cloud services and pla...
PPTX
High Availability in Microsoft Azure
PPTX
Windows azure active directory
PPTX
Windows Azure Active Directory
PPT
Mef with meta data and lazy loading
Certifications for Azure Developers
Azure Functions - Serverless Computing
Azure App Service for Windows Container
Chat application with Azure SignalR Service
Implementing enterprise cloud scenarios with Microsoft cloud services and pla...
High Availability in Microsoft Azure
Windows azure active directory
Windows Azure Active Directory
Mef with meta data and lazy loading

Recently uploaded (20)

PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
RMMM.pdf make it easy to upload and study
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
GDM (1) (1).pptx small presentation for students
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Cell Structure & Organelles in detailed.
PDF
01-Introduction-to-Information-Management.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Cell Types and Its function , kingdom of life
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Pharma ospi slides which help in ospi learning
Pharmacology of Heart Failure /Pharmacotherapy of CHF
RMMM.pdf make it easy to upload and study
O7-L3 Supply Chain Operations - ICLT Program
GDM (1) (1).pptx small presentation for students
102 student loan defaulters named and shamed – Is someone you know on the list?
Cell Structure & Organelles in detailed.
01-Introduction-to-Information-Management.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Microbial disease of the cardiovascular and lymphatic systems
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Renaissance Architecture: A Journey from Faith to Humanism
PPH.pptx obstetrics and gynecology in nursing
Final Presentation General Medicine 03-08-2024.pptx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Cell Types and Its function , kingdom of life
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Basic Mud Logging Guide for educational purpose
Pharma ospi slides which help in ospi learning

MVC 3-RAZOR Validation

  • 1. MVC 3-RAZOR-Validations Krunal Trivedi Corporate Trainer DotNet,Silverlight,SPS 2010, SharePoint 15,MVC , Windows Phone , Windows 8 MCT S For .NET Web Technology MCTS For SharePoint Server 2010 Development Co-Founder at Aavid Technologies Email:krunaltrivedi@live.in Contact : 09998472789
  • 2. MVC 3 Web Applicaiton
  • 6. Right Click on Index Action-Create View Strongly-Typed View
  • 7. Modify CustomerClass as shown below… We are going to implement Validaiton
  • 8. Create one directoryUtilityAdd New ClassAgeValidation.cs
  • 9. Prepare Class logic…that is going to make sure age greater than 21
  • 10. Open CustomerController Create New ActionDisplayCustomer
  • 11. Right Click on DisplayCustomerAdd ViewStrongly-Typed View
  • 12. Run the application. We are not getting proper validation messages…
  • 13. Modify Your Index.cshtml view as shown below…
  • 14. Now , Run Application. We get proper validation messages.
  • 17. • Direct Approach to Validation is.. – You can Directly right down validation logic inside Controller Action… – Let’s try…
  • 18. Modify CustomerClass.cs as shown below….Comment out validations attributes for FirstName and LastName
  • 19. Modify DisplayCustomer Action of CustomerController
  • 20. Run Application… For FirstName and LastName we are getting validation messages from DisplayCustomer Action.
  • 21. We will use @Html.ValidationSummary(true) It is used when there is some problem arising from an interaction between two or more property values.
  • 22. Modify Display Customer Action as shown below.
  • 24. • In this case , it is not possible to display Property-Level validation messages. • Suppose , we want to display both Property-Level validation messages and model-level validation messages. • Let’s try…
  • 25. Modify your Index.cshtml as shown below…
  • 29. Client-Side Validation • The MVC Framework supports unobtrusive client-side validation. • The term unobtrusive means that validation rules are expressed using attributes added to the HTML elements that we generate. • These are interpreted by a JavaScript library that is included as part of the MVC Framework .
  • 30. Unobstrusive means….3 things • The first is that the JavaScript that performs the validation is kept separate from the HTML elements, which means we don’t have to include client-side validation logic into our views and that the HTML we generate is easier to read.
  • 31. • The second characteristic is that the validation is performed using progressive enhancement. • This means that if a user’s browser doesn’t support all of the JavaScript features we require for client-side validation, then the validation will be performed using simpler techniques. • For example, if the user has disabled JavaScript, then server- side validation will be seamlessly performed without the user being otherwise penalized (no unpleasant error messages or special steps to take).
  • 32. • The third characteristic is a set of best practices to mitigate the effect of browser inconsistencies and behaviors.
  • 33. Enabling-Disabling Client-Side Validation Open web.config and modify appsetting as shown below.
  • 34. Referencing the JavaScript Libraries Required for Client-Side Validation Open _Layout.cshtml and observer the libraries added
  • 35. From Scriptsadd following .js file to _Layout.cshtml
  • 36. • That’s it… • Run the application… • It works Right???
  • 37. The order in which the jQuery files are referenced is significant. If you change the order, you will find that the client validation is not performed. • The scripts folder contains two versions of each JavaScript library. • The versions whose name ends with min.js are the minimized versions, meaning that all of the whitespace, comments, and other nonessential content have been removed to reduce the size of the library file. • The minimized files can be much smaller and are typically used in production to reduce the amount of data that the client downloads. • During development, the unminimized versions are typically used so that the JavaScript can be debugged (or just read) if problems arise.
  • 38. We required…one more library..that is really important…
  • 39. Run application… Observer the client-side validation.
  • 40. Client and Server side Validation messages.
  • 42. How it works !!! • One of the benefits of using the MVC Framework client-side validation feature is that we don’t have to write any JavaScript. Instead, the validation rules are expressed using HTML attributes. Here is the • HTML that is rendered by the Html.EditorFor helper for the ClientName property when client-side • validation is disabled:
  • 43. Disable the Client-Side Validation as shown below…. Run Application.
  • 47. Enable Client-side validation.. By default it is enabled..so need to write following line.. If you write…..Cool…no issues…
  • 48. Now generate the validation and check the page source.
  • 49. • The MVC client-side validation support doesn’t generate any JavaScript or JSON data to direct the validation process; like much of the rest of the MVC Framework, we rely on convention. • The first attribute that was added is data-val. The jQuery Validation library identifies those fields that require validation by looking for this attribute. • Individual validation rules are specified using an attribute in the form data-val-<name>, where name is the rule to be applied. So, for example, the Required attribute
  • 50. • The interpretation of the required and length validation rules is provided by the jQuery Validation library, on which the MVC client validation features are built.
  • 51. • The MVC client-validation features are built on top of the jQuery Validation library, and if you prefer, you can use the Validation library directly and ignore the MVC features.