SlideShare uma empresa Scribd logo
1 de 52
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
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.
• 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…
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.
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.
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.

Mais conteúdo relacionado

Mais procurados (20)

Cache control directive
Cache control directiveCache control directive
Cache control directive
 
Asp.Net Ajax Component Development
Asp.Net Ajax Component DevelopmentAsp.Net Ajax Component Development
Asp.Net Ajax Component Development
 
SOAP-UI The Web service Testing
SOAP-UI The Web service TestingSOAP-UI The Web service Testing
SOAP-UI The Web service Testing
 
Introduction to asp
Introduction to aspIntroduction to asp
Introduction to asp
 
Ch3 server controls
Ch3 server controlsCh3 server controls
Ch3 server controls
 
Ajax
AjaxAjax
Ajax
 
2310 b 06
2310 b 062310 b 06
2310 b 06
 
MVC Training Part 2
MVC Training Part 2MVC Training Part 2
MVC Training Part 2
 
Web Services Testing
Web Services TestingWeb Services Testing
Web Services Testing
 
Ajax control tool kit
Ajax control tool kitAjax control tool kit
Ajax control tool kit
 
Controls in asp.net
Controls in asp.netControls in asp.net
Controls in asp.net
 
Server Controls of ASP.Net
Server Controls of ASP.NetServer Controls of ASP.Net
Server Controls of ASP.Net
 
Web controls
Web controlsWeb controls
Web controls
 
Ajax Highlights
Ajax HighlightsAjax Highlights
Ajax Highlights
 
Controls
ControlsControls
Controls
 
ASP.NET 03 - Working With Web Server Controls
ASP.NET 03 - Working With Web Server ControlsASP.NET 03 - Working With Web Server Controls
ASP.NET 03 - Working With Web Server Controls
 
2310 b 17
2310 b 172310 b 17
2310 b 17
 
ASP .NET MVC
ASP .NET MVC ASP .NET MVC
ASP .NET MVC
 
Web services testing
Web services testingWeb services testing
Web services testing
 
Webservices testing using SoapUI
Webservices testing using SoapUIWebservices testing using SoapUI
Webservices testing using SoapUI
 

Destaque

On the incarnation
On the incarnationOn the incarnation
On the incarnationEhab Roufail
 
Institutional Presentation 1Q16
Institutional Presentation 1Q16Institutional Presentation 1Q16
Institutional Presentation 1Q16TIM RI
 
Slideshare
SlideshareSlideshare
SlideshareAnitaW
 
Frases
FrasesFrases
Frasesrucna
 
How to Create Multilingual Websites
How to Create Multilingual WebsitesHow to Create Multilingual Websites
How to Create Multilingual WebsitesChempetitive Group
 
Como o Design de I
Como o Design de IComo o Design de I
Como o Design de ISpreewell
 
The of Social Media - Women in Management
The of Social Media - Women in ManagementThe of Social Media - Women in Management
The of Social Media - Women in ManagementThomas Clifford
 
Social media 101 kimberly high
Social media 101 kimberly highSocial media 101 kimberly high
Social media 101 kimberly highThomas Clifford
 
TIM FIber Day Presentation
TIM FIber Day PresentationTIM FIber Day Presentation
TIM FIber Day PresentationTIM RI
 
Results presentation 2 q14 eng
Results presentation 2 q14 engResults presentation 2 q14 eng
Results presentation 2 q14 engTIM RI
 
Cognitive Enterprise Services
Cognitive Enterprise ServicesCognitive Enterprise Services
Cognitive Enterprise ServicesHamid Motahari
 
Yet anothr test of slide share
Yet anothr test of slide shareYet anothr test of slide share
Yet anothr test of slide shareWilliam Hart
 
Meeting with investors of march 2013
Meeting with investors of march 2013Meeting with investors of march 2013
Meeting with investors of march 2013TIM RI
 
Tim meeting with investors - sep 2012
Tim   meeting with investors - sep 2012Tim   meeting with investors - sep 2012
Tim meeting with investors - sep 2012TIM RI
 
August 10th, 2009 Dan Day ifbyphone
August 10th, 2009 Dan Day ifbyphoneAugust 10th, 2009 Dan Day ifbyphone
August 10th, 2009 Dan Day ifbyphoneStraight North
 
Press Release 1 Q03 Tele Celular Sul En
Press Release 1 Q03   Tele Celular Sul EnPress Release 1 Q03   Tele Celular Sul En
Press Release 1 Q03 Tele Celular Sul EnTIM RI
 
Edisi19mei nas
Edisi19mei nasEdisi19mei nas
Edisi19mei nasepaper
 

Destaque (20)

On the incarnation
On the incarnationOn the incarnation
On the incarnation
 
Institutional Presentation 1Q16
Institutional Presentation 1Q16Institutional Presentation 1Q16
Institutional Presentation 1Q16
 
Slideshare
SlideshareSlideshare
Slideshare
 
Frases
FrasesFrases
Frases
 
How to Create Multilingual Websites
How to Create Multilingual WebsitesHow to Create Multilingual Websites
How to Create Multilingual Websites
 
Transforming kyoto
Transforming kyotoTransforming kyoto
Transforming kyoto
 
Como o Design de I
Como o Design de IComo o Design de I
Como o Design de I
 
The of Social Media - Women in Management
The of Social Media - Women in ManagementThe of Social Media - Women in Management
The of Social Media - Women in Management
 
Social media 101 kimberly high
Social media 101 kimberly highSocial media 101 kimberly high
Social media 101 kimberly high
 
TIM FIber Day Presentation
TIM FIber Day PresentationTIM FIber Day Presentation
TIM FIber Day Presentation
 
Results presentation 2 q14 eng
Results presentation 2 q14 engResults presentation 2 q14 eng
Results presentation 2 q14 eng
 
Cognitive Enterprise Services
Cognitive Enterprise ServicesCognitive Enterprise Services
Cognitive Enterprise Services
 
Yet anothr test of slide share
Yet anothr test of slide shareYet anothr test of slide share
Yet anothr test of slide share
 
Jihad Internet - Ecommerce
Jihad Internet - EcommerceJihad Internet - Ecommerce
Jihad Internet - Ecommerce
 
Meeting with investors of march 2013
Meeting with investors of march 2013Meeting with investors of march 2013
Meeting with investors of march 2013
 
Tim meeting with investors - sep 2012
Tim   meeting with investors - sep 2012Tim   meeting with investors - sep 2012
Tim meeting with investors - sep 2012
 
August 10th, 2009 Dan Day ifbyphone
August 10th, 2009 Dan Day ifbyphoneAugust 10th, 2009 Dan Day ifbyphone
August 10th, 2009 Dan Day ifbyphone
 
Press Release 1 Q03 Tele Celular Sul En
Press Release 1 Q03   Tele Celular Sul EnPress Release 1 Q03   Tele Celular Sul En
Press Release 1 Q03 Tele Celular Sul En
 
ICWL 2009
ICWL 2009ICWL 2009
ICWL 2009
 
Edisi19mei nas
Edisi19mei nasEdisi19mei nas
Edisi19mei nas
 

Semelhante a MVC 3-RAZOR Validation

Semelhante a MVC 3-RAZOR Validation (20)

Mvc part 1
Mvc part 1Mvc part 1
Mvc part 1
 
Client control
Client controlClient control
Client control
 
Model view controller (mvc)
Model view controller (mvc)Model view controller (mvc)
Model view controller (mvc)
 
Client and server
Client and serverClient and server
Client and server
 
JavaScript
JavaScriptJavaScript
JavaScript
 
Valentine with Angular js - Introduction
Valentine with Angular js - IntroductionValentine with Angular js - Introduction
Valentine with Angular js - Introduction
 
Mvc architecture
Mvc architectureMvc architecture
Mvc architecture
 
Introduction of ASP.NET MVC and AngularJS
Introduction of ASP.NET MVC and AngularJSIntroduction of ASP.NET MVC and AngularJS
Introduction of ASP.NET MVC and AngularJS
 
Automated Acceptance Testing Example
Automated Acceptance Testing ExampleAutomated Acceptance Testing Example
Automated Acceptance Testing Example
 
Angular - Chapter 3 - Components
Angular - Chapter 3 - ComponentsAngular - Chapter 3 - Components
Angular - Chapter 3 - Components
 
MVC Framework
MVC FrameworkMVC Framework
MVC Framework
 
Mvc 130330091359-phpapp01
Mvc 130330091359-phpapp01Mvc 130330091359-phpapp01
Mvc 130330091359-phpapp01
 
CTTDNUG ASP.NET MVC
CTTDNUG ASP.NET MVCCTTDNUG ASP.NET MVC
CTTDNUG ASP.NET MVC
 
Chapter5-Bypass-ClientSide-Control-Presentation.pptx
Chapter5-Bypass-ClientSide-Control-Presentation.pptxChapter5-Bypass-ClientSide-Control-Presentation.pptx
Chapter5-Bypass-ClientSide-Control-Presentation.pptx
 
Introduction to Angular Js
Introduction to Angular JsIntroduction to Angular Js
Introduction to Angular Js
 
Introduction to Angularjs : kishan kumar
Introduction to Angularjs : kishan kumarIntroduction to Angularjs : kishan kumar
Introduction to Angularjs : kishan kumar
 
Introduction to Angularjs
Introduction to AngularjsIntroduction to Angularjs
Introduction to Angularjs
 
Spring Framework-II
Spring Framework-IISpring Framework-II
Spring Framework-II
 
Ajaxppt
AjaxpptAjaxppt
Ajaxppt
 
MVC 4
MVC 4MVC 4
MVC 4
 

Mais de Krunal Trivedi

Certifications for Azure Developers
Certifications for Azure DevelopersCertifications for Azure Developers
Certifications for Azure DevelopersKrunal Trivedi
 
Azure Functions - Serverless Computing
Azure Functions - Serverless ComputingAzure Functions - Serverless Computing
Azure Functions - Serverless ComputingKrunal Trivedi
 
Azure App Service for Windows Container
Azure App Service for Windows ContainerAzure App Service for Windows Container
Azure App Service for Windows ContainerKrunal Trivedi
 
Chat application with Azure SignalR Service
Chat application with Azure SignalR ServiceChat application with Azure SignalR Service
Chat application with Azure SignalR ServiceKrunal Trivedi
 
Implementing enterprise cloud scenarios with Microsoft cloud services and pla...
Implementing enterprise cloud scenarios with Microsoft cloud services and pla...Implementing enterprise cloud scenarios with Microsoft cloud services and pla...
Implementing enterprise cloud scenarios with Microsoft cloud services and pla...Krunal Trivedi
 
High Availability in Microsoft Azure
High Availability in Microsoft AzureHigh Availability in Microsoft Azure
High Availability in Microsoft AzureKrunal Trivedi
 
Windows azure active directory
Windows azure active directoryWindows azure active directory
Windows azure active directoryKrunal Trivedi
 
Windows Azure Active Directory
Windows Azure Active DirectoryWindows Azure Active Directory
Windows Azure Active DirectoryKrunal Trivedi
 
Mef with meta data and lazy loading
Mef with meta data and lazy loadingMef with meta data and lazy loading
Mef with meta data and lazy loadingKrunal Trivedi
 

Mais de Krunal Trivedi (9)

Certifications for Azure Developers
Certifications for Azure DevelopersCertifications for Azure Developers
Certifications for Azure Developers
 
Azure Functions - Serverless Computing
Azure Functions - Serverless ComputingAzure Functions - Serverless Computing
Azure Functions - Serverless Computing
 
Azure App Service for Windows Container
Azure App Service for Windows ContainerAzure App Service for Windows Container
Azure App Service for Windows Container
 
Chat application with Azure SignalR Service
Chat application with Azure SignalR ServiceChat application with Azure SignalR Service
Chat application with Azure SignalR Service
 
Implementing enterprise cloud scenarios with Microsoft cloud services and pla...
Implementing enterprise cloud scenarios with Microsoft cloud services and pla...Implementing enterprise cloud scenarios with Microsoft cloud services and pla...
Implementing enterprise cloud scenarios with Microsoft cloud services and pla...
 
High Availability in Microsoft Azure
High Availability in Microsoft AzureHigh Availability in Microsoft Azure
High Availability in Microsoft Azure
 
Windows azure active directory
Windows azure active directoryWindows azure active directory
Windows azure active directory
 
Windows Azure Active Directory
Windows Azure Active DirectoryWindows Azure Active Directory
Windows Azure Active Directory
 
Mef with meta data and lazy loading
Mef with meta data and lazy loadingMef with meta data and lazy loading
Mef with meta data and lazy loading
 

Último

Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptxDhatriParmar
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptxmary850239
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptxmary850239
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17Celine George
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
CHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptxCHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptxAneriPatwari
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 

Último (20)

Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
CHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptxCHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptx
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 

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
  • 3.
  • 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.
  • 15.
  • 16.
  • 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…
  • 28.
  • 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.
  • 41.
  • 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.
  • 45.
  • 46.
  • 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.