Hello,
There is a plenty of very good post and explanations how to implement validation with ASP.NET MVC, and I prefer one of these:
However, I really like to call ActionMethods via jquery $.ajax method. One of the reasons why I want to use $.ajax is because there will be a lot of partial views loaded into the page dynamically (even the form for entity creation) via $.ajax calls and I can't just return the view - I'll lose all dynamically loaded content.
To give you a better view on the problem, I'll post some simple code to explain how would I like to call controllers actions and handle responses in client, jquery code.
The controllers ActionMethod:
public ActionResult CreateCustomer(string name, string accountNumber)
{
try
{
CustomerService.InsertCustomer(name, accountNumber);
return Json(new ActionInfo()
{
Success = true,
Message = "Customer Is Successfully Created"
});
}
catch (Exception ex)
{
return Json(new ActionInfo()
{
Success = false,
Message = ex.Message
});
}
}
Calling and handling in client code:
$.ajax({
type: "POST",
url: $form.attr('action'),// /MyController/CreateCustomer
data: $form.serialize(),
error: HandleUnespectedError,
dataType: "json",
success: function(response) {
if (response.Success)
alert("Success: " + response.Message);
else
alert("Error: " + response.Message);
}});
Is there a good way to make some of these validation frameworks to work the way I need? I know that I can add validation errors in ActionInfo, and then handle it in client, but that would be already a building of my one validation, I believe.