I’ve created a simple login form that I’d like to validate via ajax.
What’s the most appropriate method to display the server-side validation errors produced by MVC? Is converting the error to a JSON result and stripping out the error message appropriate? If not, then what would be?
Currently what I have is posting correctly, but the entire form comes back. The goal is just to display the server-side errors on the form without a postback/refresh. Thanks in advance…here is my code:
Main View – Login.vbhtml
@ModelType UHCO_MVC_Agency.LoginModel
<div class="error-container">@Html.ValidationSummary(True)</div>
<div class="row-fluid">
<div class="span12">
<div id="login-form" class="span6">
@Using Html.BeginForm()
@<fieldset>
<legend>Log in to your account now</legend>
<div class="row-fluid">
<div class="span12">
@Html.LabelFor(Function(m) m.UserName)
@Html.TextBoxFor(Function(m) m.UserName, New With {.class = "span12", .placeholder = "Username"})
@Html.ValidationMessageFor(Function(m) m.UserName)
</div>
</div>
<div class="row-fluid">
<div class="span12">
<label for="Password">Your password</label>
@Html.PasswordFor(Function(m) m.Password, New With {.class = "span12", .placeholder = "Password", .type = "password"})
@Html.ValidationMessageFor(Function(m) m.Password)
</div>
</div>
<div class="row-fluid">
<div class="span12">
<label for="RememberMe" class="checkbox clearfix">
@Html.CheckBoxFor(Function(m) m.RememberMe)
Remember me next time I visit
</label>
</div>
</div>
<button type="submit" class="btn btn-primary input-small" value="submit">Log in</button>
</fieldset>
End Using
</div>
</div>
</div>
Controller – AccountController.vb
<HttpPost()> _
Public Function Login(ByVal model As LoginModel, ByVal Username As String, ByVal Password As String, ByVal returnUrl As String) As ActionResult
Dim res As New LoginResponse()
Try
'login here
If Not String.IsNullOrEmpty(returnUrl) AndAlso Url.IsLocalUrl(returnUrl) Then
res.Status = "Success"
Return RedirectToAction("Welcome", "Account")
End If
Catch ex As Exception
If Not HttpContext.Request.IsAjaxRequest() Then
ModelState.AddModelError("", ExceptionWrapper.Wrap(ex).ExceptionMessage())
Return View("Login", model)
Else
res.Status = "Failed"
res.Errors.Add(ExceptionWrapper.Wrap(ex).ExceptionMessage())
Return Json(res)
End If
End Try
' If we got this far, something failed, redisplay form
Return View(model)
End Function
Application.js
$("#login-form form").live().submit(function (e) {
e.preventDefault();
alert("ok");
});
This is a C# version of the solution. I believe, it should be easy to convert to corresponding VB.NET version
using JSON is absolutely a nice way to do this.
First thing. I would change the
POSTaction method to handle my request for Ajax requests and returnJSONresponse back.To hold my Error response, I will create a class like this
And in our
POSTaction methodSo if there is some error you want to return back to the client, you will send JSON in this format
And If everything is fine, We will send JSON like this
Now in our view, we will get rid of the Ajax form and use the Normal form tag with some pure jQuery.
EDIT : Change your js code like this and see what happens