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.
- the
defaultmodelbinder
initializes new instance of model - the
defaultmodelbinder
reads form data (name/value pairs) , if property name matches 1 of form data values, property set (assuming valid) , value addedmodelstate
. if value not valid, property not set value addedmodelstate
(theattemptedvalue
) alongmodelstate
error - when return view,
@html.validationmessagefor()
method readsmodelstate
values , if there error associated property, error message added html generatedvalidationmessagefor()
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
Post a Comment