0

Using aspnet mvc2 with the model auto-generated by entity framework:

Is it possible to tell entity framework to auto-annotate all fields? eg:

If database field says not null then add [Required] If DB field is a nvarchar(x) then add [StringLength(x)]

And so on?

What if the field name contains the string "email" eg CustomerEmail - can I get EF to auto-annotate that with an appropriate annotation ([Regex()] maybe)

As I understand it, if the model fields are annotated, and I use both Html.ValidationMessageFor() and use if (ModelState.IsValid) in my controller, then that is all I need to do to have basic clientside input validation working?

Thanks

2
  • possible duplicate of stackoverflow.com/questions/2330573/… Commented Apr 28, 2010 at 12:59
  • Yes, its a duplicate - I didn't see the older question because its question and tags are too vague. The answer in that question hasn't helped either. Commented Apr 28, 2010 at 21:17

2 Answers 2

1

I am not aware of any automatically generated attributes.

As I understand it, if the model fields are annotated, and I use both Html.ValidationMessageFor() and use if (ModelState.IsValid) in my controller, then that is all I need to do to have basic clientside input validation working?

That is correct. http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-2-modelmetadata.html

Sign up to request clarification or add additional context in comments.

3 Comments

Its seems like it would be such a common requirement. Eg The field in the database is nvarchar(20), so you would expect to be able to somehow be able to get an automatic annotation of [StringLength(20)]. To have to manually type the annontation for each and every field would be very wrong.
I saw your new blog post weblogs.asp.net/rajbk/archive/2010/05/04/… solving this problem .. thanks a lot and +1 :)
JK, the post is an early attempt. For example, you cannot make any changes to the auto generated fields. You could copy the generated class into another class but you have to do the extra work of making sure your classes are in sync.
0

in jQuery, you can run through every object in a page. If it's client side validation you're looking for, you can use jQuery's field validation.

2 Comments

With 50+ tables and many hundreds of fields, any solution must be automatic. It would be wrong to manually type any code when the database already contains the metadata describing if the field is required, the field length and so on.
you can separate these fields from one another by applying classes to the objects. for say, you search every input field there is, then locate if this field has, let's say, a "chumbawamba" class name in it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.