ASP.NET MVC has great ajax integration with no doubt. Simply by using ajax forms you can use partial page rendering and there are also controller callbacks. One thing that will break if you switch to Ajax / JsonResult is the built-in Validation result (ModelErrors) rendering of the MVC engine.

Ajax Validation

It won’t work because of a simple fact: The built-in validation will take place when the page is being rendered on the server. When you use ajax and callbacks page will not re-render on the server-side, hence validation can not mark fields with error and add validation information to the already rendered page.

Let’s see how you can do this, without using a client-side validation framework.

Creating a Contact Form

First, let’s create a form using Ajax.BeginForm and provide a javascript action to process the results. You can get rid of the ValidationMessage and ValidationSummary, since we don’t need it anymore:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<% using (Ajax.BeginForm(new AjaxOptions { OnComplete = "ProcessResult" })) { %>
<p>
<label for="Name" class="contactLabel">Your Name:</label>
<%= Html.TextBox("SenderName", Model.SenderName)%>
</p>
<p>
<label for="Email" class="contactLabel">Your Email:</label>
<%= Html.TextBox("Email", Model.Email)%>
</p>
<p>
<label for="Subject" class="contactLabel">Subject:</label>
<%= Html.TextBox("Subject", Model.Subject)%>
</p>
<p>
<label for="Message" class="contactLabel">Message:</label>
<%= Html.TextBox("Message", Model.Message)%>
</p>
<p>
<br />
<input type="submit" value="Send" class="inputButton" />
<br />
</p>
<div id="operationMessage"><ul></ul></div>
<% } %>

Notice that there is a div at the bottom which acts as an error placeholder and will be used to display validation information.

To actually bind these fields to data, we need to create an POCO class acting as a backing entity. In this example, I’ve used a poco class and decorate it with NHibernate Validator‘s attributes to do the validation. See here for more info on how to configure NHibernate Validator using ASP.NET MVC:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class ContactMessage : IValidatable
{
public ContactMessage()
{
MessageDate = DateTime.Today;
}

public virtual int Id
{
get; private set;
}

[NotNullNotEmpty]
public virtual string SenderName
{
get; set;
}

[NotNullNotEmpty]
[Email]
public virtual string Email
{
get; set;
}

[NotNullNotEmpty]
public virtual string Subject
{
get; set;
}

[NotNullNotEmpty]
public virtual string Message
{
get; set;
}

public virtual DateTime MessageDate
{
get; private set;
}
}

Controller Action

Two methods are needed to handle to Post / Get separately. Our Get method returns a new instance of our contact entity to the view, and Post method is where the operation results is returned to the client using Json. The OperationResultInfo class containing the elaborate error information is what returned as Json to the client:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
public class OperationResultInfo
{
public OperationResultInfo()
{
Errors = new List<ErrorReasonInfo>();
}

public bool Successfull { get; set; }

public string Message { get; set; }

public IList<ErrorReasonInfo> Errors { get; set; }
}

public class ErrorReasonInfo
{
public string PropertyName { get; set; }

public string Error { get; set; }
}

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Contact()
{
return View(new ContactMessage());
}

[AcceptVerbs(HttpVerbs.Post)]
[AutoValidate]
public ActionResult Contact(ContactMessage msg)
{
if (ModelState.IsValid)
{
try
{
this.messengerService.SendMail(msg);

return Json(new OperationResultInfo
{
Successfull = true,
Message = "Thank you. Your message was sent successfully."
});
}
catch (Exception ex)
{
ModelState.AddUnhandledError(ex);
return Json(new OperationResultInfo
{
Successfull = false,
Message = "There was an error processing your request. Retry later."
});
}
}

return Json(new OperationResultInfo
{
Successfull = false,
Message = "Could not send your information.",
Errors = ModelState.GetAllErrors()
});
}

public static class ModelStateExtensions
{
public static IList<ErrorReasonInfo> GetAllErrors(this ModelStateDictionary modelState)
{
var errors = new List<ErrorReasonInfo>();

foreach (var state in modelState)
{
if (state.Value.Errors.Count &gt; 0)
{
var err = new ErrorReasonInfo { PropertyName = state.Key };
state.Value.Errors.ForEach(x => err.Error += x.ErrorMessage);

errors.Add(err);
}
}

return errors;
}
}

Displaying The Json Result

The Json result returned to the client contains all the fields having errors and the related error message. A typical result would look like this:

1
2
3
4
5
6
7
8
9
10
{
"Successfull":false, "Message":"Could not send your information:",
"Errors":
[
{"PropertyName":"SenderName", "Error":"may not be null or empty"},
{"PropertyName":"Email", "Error":"may not be null or empty"},
{"PropertyName":"Subject", "Error":"may not be null or empty"},
{"PropertyName":"Message", "Error":"may not be null or empty"}
]
}

How do we format this data on the client and display it in a user-friendly fashion on our page? Let’s process the Json object using javascript and add the result to the error place holder using jQuery:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<script type="text/javascript">
function ProcessResult(content) {

var json = content.get_response().get_object();
var result = eval(json);

$("#operationMessage > span").empty();
$("#operationMessage > ul").empty();
$(':input').removeClass('input-validation-error');

if (result.Successfull) {
$('#operationMessage').append('<span>' + result.Message + '</span>')
.removeClass('error')
.addClass('success');
}
else {

$('#operationMessage').append('<span><br>' + result.Message + '</span>')
.removeClass('success')
.addClass('error');

for (var err in result.Errors) {
var propertyName = result.Errors[err].PropertyName;
var errorMessage = result.Errors[err].Error;
var message = propertyName + ' ' + errorMessage;

$('#' + propertyName).addClass('input-validation-error');
$('#operationMessage > ul').append('<li># ' + message + '</li>');
}
}
}
</script>

Pretty neat, ha? Hope this helps.