We have an ASP.NET MVC project in which we use jQuery.ajax to send some form data to the controller. In some cases, the controller method is called my throwing in exceptions due to a bad SQL statement. I would like to handle the exception in TRY CATCH and pass in details of why the SQL query was hacked. Since I am handling the exception, the jQuery.ajax post gets HTTP 200 back from the server, so no errors occur. If I use the jQuery.ajax complete property, it seems that I can look for the details of the exception and show the user, but I'm not sure how I can do this from the controller side. At first 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 ajax call.
Any ideas how I can pass the result back to an ajax call from my controller?
Tks
source share