0

I build MVC3 app with EF 4.3 (database first) using DBContext Generator and using Scaffolding options template to create controller with its views so it creates a view with the model (data annotations) client validation depending on the properties of fields in db.

the client validation for 'Field Required' work fine with numeric fields not with nvarchar fields (at least in my case) my case,

table/class -> Table1
    id
    Name -> nvarchar(50) -> not allow null
    Count -> int -> not allow null

the validation rule 'Required' works fine with 'Count' field, but not with 'Name' field

is there any idea why??

Jake1164
  • 12,291
  • 6
  • 47
  • 64
Shady
  • 364
  • 5
  • 18
  • Can you post your data annotations validation attributes and fields? – Fabio Milheiro Jun 19 '12 at 15:04
  • @FabioMilheiro, i didn't write any attributes for data annotations, i depend on the attributes of the fields in DB (Ex: when the field is NotNull 'Allow Nulls = No' means that it is required also when the field is int so the field must be a number). – Shady Jun 19 '12 at 15:18
  • Mmm.. sorry I was inclined to think it was more data annotations than code first. Never used code first so good luck – Fabio Milheiro Jun 19 '12 at 15:20

1 Answers1

0

Does your model contain [Required()]? Does your webpage contain the datal-val* attributes on your string fields?

If you want client side validation you should use attributes, or IValidateableObject interface for custom validation or the fluent api (note the last two are server side only but can display messages to the client if handled) Don't rely on the db errors bubbling back (if thats what you were doing from my understanding above)

Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
  • Do you mean that not support to this option, i'd like to retrieve data annotations from database for simple constraints like length and nullability. – Shady Jun 20 '12 at 10:15
  • if you use the Entity Framework Power Tools it will query the database and create your code first model. Check out my video here: http://channel9.msdn.com/Events/TechEd/NorthAmerica/2012/DEV215 – Adam Tuliper Jun 20 '12 at 14:51
  • After installing the power tools, right click in the project and choose "Entity Framework" menu on the context menu and then "Reverse Engineer Code First". It will then create your classes and in addition the mapping class for the fluent api. NOTE this will not provide client validation, it must round trip to the server. If using MVC 4 it will catch these validations on the server, if mvc3 it won't and you'll need an action filter to handle those exceptions. If you want client side, you'll have to add the data annotations manually. Im not aware of anything to generate them for you – Adam Tuliper Jun 20 '12 at 14:52