In this article I will explain with an example, how to return Exception Error Message when using JSON and AJAX in ASP.Net MVC Razor.
This article will illustrate how to return the Exception Error Message using JSON from the Controller when calling the Controller using jQuery AJAX in ASP.Net MVC Razor.
Note: For beginners in jQuery AJAX and ASP.Net MVC, please refer my article ASP.Net MVC: jQuery AJAX and JSON Example.
The Controller consists of two Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
Action method for handling AJAX POST operation
This Action method handles the AJAX Form submission and it accepts the value of the Form elements as parameter.
The value of the Age is converted into an Integer. If this conversion is successful, then a JSON object with a property result set with Boolean value TRUE is returned.
In case when the value of Age is not a valid Integer value, an exception occurs.
Then a JSON object with properties i.e. error (containing the exception message) and result (set with Boolean FALSE value) is returned.
public class HomeController : Controller
    public ActionResult Index()
        return View();
    public ActionResult AjaxMethod(string name, string age)
            int personAge = int.Parse(age);
            return Json(new { result = true });
        catch (Exception ex)
            return Json(new { result = false, error = ex.Message });
The View consists of two TextBox fields created for capturing values for Name and Age.
There’s also an HTML Button at the end of the Table which has been assigned with a JavaScript OnClick event handler.
When the Submit Button is clicked, the value of Name and Age TextBoxes are passed to the AjaxMethod Action method using jQuery AJAX call.
Once the response is received, the value of the result property is evaluated.
If the value is TRUE, then a success message is displayed using JavaScript Alert Message Box.
And if the value is FALSE, the value of the error property is displayed using JavaScript Alert Message Box.
    Layout = null;
<!DOCTYPE html>
    <meta name="viewport" content="width=device-width"/>
            <td>Name: </td>
            <td><input type="text" id="txtName"/></td>
            <td>Age: </td>
            <td><input type="text" id="txtAge"/></td>
            <td><input type="button" value="Submit" onclick="AjaxFormSubmit()"/></td>
    <script type="text/javascript" src=""></script>
    <script type="text/javascript" src=""></script>
    <script type="text/javascript">
        function AjaxFormSubmit() {
            var person = {};
            person.Name = $("#txtName").val();
            person.Age = $("#txtAge").val();
                type: "POST",
                url: "/Home/AjaxMethod",
                data: JSON.stringify(person),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    if (response.result == true) {
                        alert("Form submitted!");
                    else {
ASP.Net MVC: Return Exception Error Message when using JSON and AJAX
Browser Compatibility

The above code has been tested in the following browsers.

Internet Explorer  FireFox  Chrome  Safari  Opera 

* All browser logos displayed above are property of their respective owners.