views:

64

answers:

1

Hi,

Before down-voting or closing for duplicate questions, I want to make clear that I have read old posts on this site and others. The problem is that they are old (some at least) and are not giving really good examples.

I currently use ASP.Net MVC 2.0 with Ajax.BeginForm and jQuery Validation client-side validation which both works perfectly fine. But I would like server-side validation errors to be displayed like client-side errors (not necessarily remote). The server-side errors are currently applied with ModelState.AddModelError.

I use code which extends something like this:

<% Html.EnableClientValidation(); %>

using (Ajax.BeginForm("Bar", new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "result"})) { %>
<fieldset>
    <p>
        <%: Html.TextAreaFor(model => model.Foo) %>
        <%: Html.ValidationMessageFor(model => model.Foo) %>
    </p>
    <p>
        <input type="submit"/>
    </p>
</fieldset>

And, as I mentioned, I set the errors with ModelState.AddModelError. Currently, I return a validation-summary on error (which does not bind to the elements which causes them) and otherwise return my result.

Until now, I have encountered multiple solutions to this problem:

  • xVal (supports remote server-validation)
  • Set the entire form in a RenderPartial and id='result'.
  • Return some JSON with result and error-messages

But is there really not a better solution (maybe in ASP.Net MVC 3.0)? Is xVal still a good solution (not a lot of development is going on)?

If I go with a JSON solution, is there a jQuery-way to assign errors to specific elements which mimics 100% the client-side validation?

If there is any better solution, I'm prepared to switch the technologies I use.

Thanks, Lasse Espeholt

+1  A: 

xVal is good! I'm using it with 2 asp.net mvc 2 projects and it works wonderfully.

Patricia
Can it fall-back if no javascript is enabled? And, I need to check field for valid XML which takes some computation. As I understand it, it will probe some server-side validations before actually make the form-submit call, is this correct? Is it then possible to cache a result which is computed on validation so it can be used when the actual submit is invoked?
lasseespeholt
xVal does have support for validation that needs to go to the server to be checked. look up the remote validation functionality. not sure about caching.
Patricia