-
Website
http://www.emadibrahim.com -
Original page
http://www.emadibrahim.com/2008/09/08/client-server-side-validation-in-aspnet-mvc/ -
Subscribe
All Comments -
Community
-
Top Commenters
-
danatkinson
1 comment · 1 points
-
Korayem
3 comments · 1 points
-
dukon21
1 comment · 1 points
-
Mladen Mihajlovic
1 comment · 1 points
-
Chad Myers
3 comments · 1 points
-
-
Popular Threads
-
Select Random Records Using Nhibernate
6 days ago · 4 comments
-
Select Random Records Using Nhibernate
CSS classes should be used for styling and grouping similar elements e.g. you could be using the class attribute already to style certain form elements or maybe using it to group specific elements which can then be manipulated with CSS or JavaScript jQuery etc. If you had used the class method for tagging the elements, you would have limited peoples use of them for other important reasons - so good choice :)
We've rolled out a similar system (also based on Steve Sanderson's excellent work) and it's working well and is proving to be robust and extensible.
Keep up the great work and looking forward to reading your book :)
Mike
Nice post and looking good.
Which book your writing?
http://blog.codeville.net/2008/04/30/model-base...
Put this on CodePlex
the validators allows you to make a remote call... so you can easily call
an action that checks if a username is unique and return a json result for
the jquery script to parse... I haven't done it but check the plugin
documentation for examples.
http://code.google.com/p/mvcvalidation/source/c...
btw I'm looking at modifying it to use System.ComponentModel.DataAnnotations instead of castle as then I can put the validation attributes in a metadata class instead of the class to be validated. My classes are generated so putting attributes on the fields is not an option.
it to work with the Beta.
I like using the DataAnnotations namespace instead and have switched to it
in one of my projects.
I use POCO classes for models but I write conversion functions that convert
and from my POCO to my LINQ to SQL entities. Some grunt work but works
well.
btw I'm looking at modifying it to use System.ComponentModel.DataAnnotations instead of castle as then I can put the validation attributes in a metadata class instead of the class to be validated. My classes are generated so putting attributes on the fields is not an option.
Great work. I have a similiar issue to Jeremy's, but I am also doing what you are doing and abstracting the L2S data layer out and upkeeping my own model's. However I cannot set an attribute on the model to declare the validation rules as my model classes are being generated from CodeSmith.... yikes. What can I do so I can set this rules? Any ideas?
it is the same problem with using LINQ 2 SQL, you can't add attributes to
the generated code.
-Emad
I got past this problem by using System.ComponentModel.DataAnnotations, I am using a feature in Dynamic Data to achieve this (http://blogs.microsoft.co.il/blogs/noam/archive...) lucky find, but working well!
I had to update my last posted example with a few bigger changes to support this. My last code still stands if you can add attributes to your model, as said I couldn't as mine was code generated. I have just outlined the approach I took in my blog (http://goneale.wordpress.com/2009/01/19/aspnet-...) rather than spamming yours. :)
Emad, I hope you don't mind but I have proposed a change to your code at this point:
signature declaration:
public static string ClientSideValidation(this HtmlHelper htmlHelper,
string formName,
object modelToValidate,
bool prefixWithModelName)
and string builder:
This caters for typical Model Binding scenarios where you include the model name as a prefix with a dot "." and as a jquery validator plugin requirement, we need double quotes " " as seen here: http://docs.jquery.com/Plugins/Validation/Refer...
rules.AppendLine();
messages.AppendLine();
string field = (prefixWithModelName ? "\"" + modelToValidate.GetType().Name + "." + prop.Name + "\"" : prop.Name);
rules.AppendFormat("{0}: {1}", field, "{");
messages.AppendFormat("{0}: {1}", field, "{");
rules.AppendLine();
messages.AppendLine();
I also made a change to leave fields as is without ToLower() to comply with my forms and class naming standard.
I suppose to remove complexity entirely, all names could just be wrapped in double quotes though. Shrug.
This is working well for me.