开发者

Generating jquery 'rules' from business model to UI in asp.net mvc

开发者 https://www.devze.com 2022-12-26 02:05 出处:网络
I\'ve had a good look around and am certain that there\'s no matching question on SO, so here goes. Has anyone created a \'helper\' method on their model that generates jquery (or plain javascript) r

I've had a good look around and am certain that there's no matching question on SO, so here goes.

Has anyone created a 'helper' method on their model that generates jquery (or plain javascript) rules validation dynamically, based on the criteria/rules that are contained within the object and taken from a repository (i.e. DB).

What i'm thinking of is a discrete set of partial views (and associated models) that have rules at the business logic 'level' and rather than (or in combination with) validating the rule(s) at postback, translating the same rules into tightly focussed jquery methods that work identically at client (js) and server (c#) levels. I can see benefits here re performance. Also, the rules definitions could be created in a single place (in c#) and the jquery generated off of that, thus allowing single edits to update both code streams.

I appreciate that there would be limitations imposed by language specific co开发者_如何转开发nstraints but the general principle could be quite interesting if used appropriately. I'm also aware that testibility could be an issue when using two different language structures and hoping to achieve similar test outcomes - but those aside...

any thoughts or experiences of similar out there?

edit - this article goes some way towards what I was thinking of:

https://blog.stevensanderson.com/2009/01/10/xval-a-validation-framework-for-aspnet-mvc/

and also:

https://devermind.wordpress.com/2009/06/21/asp-net-mvc-tip-3-how-to-cover-all-your-client-side-form-validation-needs-without-writing-any-javascript-part1/


It depends on what you mean by "rules". If you mean basic validation, that already exists with the DataAnnotations attributes.

These attributes, when applied properly, can combine with Html.ValidateMessageFor helper methods to automatically validate input before the form is posted back.


Codebetter.com has a good tutorial with a MVC validation solution that builds the jQuery validation

http://codebetter.com/blogs/karlseguin/archive/2009/04/26/validation-part-1-getting-started.aspx


Have a look at this article, ASP.NET MVC 2 Custom Validation, by Phil Haack. In it he describes how to add client side validation for custom validation attributes.

HTHs,
Charles

0

精彩评论

暂无评论...
验证码 换一张
取 消