We have an ASP.NET MVC project in which we make use of jQuery.ajax to post some form data to a controller. In some cases, the controller method being called my run into exceptions due to a bad SQL statement. I'd like to handle the exception in a TRY CATCH and pass back to the user details about why the SQL statement bombed out. Because I'm handling the exception, the jQuery.ajax post is getting an HTTP 200 back from the server, so no errors appear to be happening. If I make use of jQuery.ajax complete property, it seems I'd be able to look for exception details and show the user, but I'm not sure how I can do this from the controller side. Initially I tried something like this on the controller side:
Public Function IndexPost(formvalues as FormCollection) as ActionResult
Try
...
Catch ex as Exception
Dim sErr as String = "Exception occurred: " & ex.ToString
ViewData("PostResult") = sErr
End Try
Return View()
End Function
And then added this to my jQuery.ajax post:
complete: function (data) {
var postDataResult = "<%=ViewData("PostResult")%>";
if (postDataResult.length>2)
{
alert("Value of post result: " + postDataResult);
}
}
ViewData("PostResult") is null of course due to the ajax call.
Any ideas how I can pass a result back to the ajax call from my controller?
Tks