c# - Which part of the code shows validation errors after Post in asp.net MVC? -


let's have 1 view model. has 1 required name property. , have disabled client-side validation. have code in action method:

     if (!modelstate.isvalid)         {             return view(model);         } 

so, works fine. highlight required field after post. but, can't understand jquery validaion function process? how, jquery validation detects form has been submitted once?

i want find code, because want change slightly. example, have own helpers, has custom validation logic. now, custom helper validation not showing after invalid post. and, want add logic built-in function, can not find anywhere.

firstly, if have disabled client side validation, jquery validation has nothing (you have disabled it!). briefly explain happens when post , return view.

  1. the defaultmodelbinder initializes new instance of model
  2. the defaultmodelbinder reads form data (name/value pairs) , if property name matches 1 of form data values, property set (assuming valid) , value added modelstate. if value not valid, property not set value added modelstate (the attemptedvalue) along modelstate error
  3. when return view, @html.validationmessagefor() method reads modelstate values , if there error associated property, error message added html generated validationmessagefor() method , relevant class name (which highlights it) added

you can inspect source code defaultmodelbinder , validationextensions if want see more detail of how these work.

as "i want find code, because want change slightly", dont. have not indicated trying do, or shown code html helper extension method, html helpers not (and should not) contain validation logic. responsible generating html based on property , validation attributes applied property.

if have custom validation logic property, create attribute inherits validationattribute (and if want client side validation needs implement iclientvalidatable). guide creating own validation attributes this article.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -