Using xVal Server Side with TryUpdateModel()

Feb 10, 2010 at 5:03 PM

A common scenario with MVC is where the model object is not passed in to the action as a parameter, but instead you get it from a repository or data access method and then use TryUpdateModel to update the existing object and save it to the database.  xVal does not seem to support this.  For example, if I emulate this in the example app using the following:

        public ActionResult PlaceBooking()
            var booking = new Booking();
            booking.NumGuests = 1;

            try {
                return RedirectToAction("Completed");
            } catch(RulesException ex) {
                ex.AddModelStateErrors(ModelState, "booking");
                return View();


If i leave NumGuests blank, it will still validate, but the error message turns to the default.  "A value is required."

I believe this is because the existing object, model binding failed so the value remained "1" on NumGuests, so xVal never saw an error.  However there was still a model binding error fired into modelstate by the mvc framework.

Is there some way around this?


Feb 10, 2010 at 5:56 PM

Hi Craig

I think you've misunderstood what xVal does. It isn't a server-side validation framework. It isn't responsible for performing any validation on the server. 

xVal is a way of generating client-side validation based on a server-side configuration. It also provides a few helper methods (including AddModelStateErrors()) that make it easier to transfer any errors that *you* have detected on the server into the ModelState dictionary so they will get displayed on the client. 



Feb 10, 2010 at 5:57 PM

By the way, please don't take my point as a criticism. Several users have had the same misconception, so I guess I need to find a better way of making it clear.

Feb 10, 2010 at 6:02 PM

Well I think i'm confused further because the problem I'm running into is an issue with input validation instead of model validation.  I need to change the default error message when model binding fails on a property, BEFORE it ever gets to data annotations model validation, castle validation, etc or your framework.