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
andid='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