This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Is there a way to automatically get my model errors into the ModelState.ModelError collection? Right now it seems I have to do the following to get this wot work properly: [AcceptVerbs(HttpVerbs.Post)] |
|
|
Hi Max, We implement IDataErrorInfo on Entity, so you can use this with UpdateModel to have it perform the validation and raise errors accordingly. We do have a specific model binder for entities on the way as well which may be of use to you :) But in the meantime you should be able to do this: [AcceptVerbs(HttpVerbs.Post)] if (!TryUpdateModel(customer)) { return View(customer); } // TODO: Add insert logic here or - alternatively, if your Customer is already being unbound correctly as an inbound parameter, you can just test the .IsValid flag on it and return it to the view.
Jeremy |
|
|
Max, Depending on the simplicity of your situation, I would just create a custom modelbinder that calls Validate() on the entity and if there are any errors, move the errors to ModelState. I wrote an example using the Validation Application Block at the following link which will help give you ideas on this: An Aha Moment on MVC Validation Extensibility in DefaultModelBinder - Bye to IDataErrorInfo A special caution when using the [ValidateUnique] Attribute. It won't fire unless you have the entity attached to a Unit Of Work, which is not always the case when binding to a new entity. Sadly, you will have to add it to a unit of work during binding and then call Validate() to get that to fire. Feels dirty doing it this way IMHO, but nothing is ever perfect ;)
The MVC Framework is probably being released this week so I look forward to any MVC custom modelbinders and anything else to help with using Lightspeed, Jeremy. With LINQ To SQL in the grave and the Entity Framework being questionable, Lightspeed is in the perfect spot to grab a lot of marketshare in the MVC arena. Regards, Dave |
|