In this article I will explain with an example, how to return NULL (Nothing) from ActionResult (Action Method) in ASP.Net MVC Razor.
There are two ways to return NULL from an ActionResult (Action Method):
1. Using EmptyResult class. In order to learn more about EmptyResult class, please refer my article ASP.Net MVC EmptyResult Example: Return NULL (Nothing) from Controller to View.
2. Simply return NULL value. In such case, ASP.Net automatically returns EmptyResult class object internally.
Note: For beginners in ASP.Net MVC, please refer my article ASP.Net MVC Hello World Tutorial with Sample Program example.
 
 
Controller
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.
First, a check is performed whether valid Form values have been submitted and if the Form values are valid a BOOLEAN value TRUE is returned else a NULL value returned.
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
 
    [HttpPost]
    public ActionResult Index(string firstName, string lastName)
    {
        if (!string.IsNullOrEmpty(firstName) && !string.IsNullOrEmpty(lastName))
        {
            string name = string.Format("Name: {0} {1}", firstName, lastName);
            return Json(true);
        }
 
        return null;
    }
}
 
 
View
The View consists of two TextBox fields created for capturing values for First Name and Last Name.
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 First Name and Last Name TextBoxes are passed to the AjaxMethod Action method using jQuery AJAX call.
Once the response is received, based on whether response is a BOOLEAN TRUE value or NULL object, appropriate message is displayed using JavaScript Alert Message Box.
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    <table>
        <tr>
            <td>First Name: </td>
            <td><input type="text"id="txtFirstName"/></td>
        </tr>
        <tr>
            <td>Last Name: </td>
            <td><input type="text" id="txtLastName"/></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="button" value="Submit" onclick="AjaxFormSubmit()"/></td>
        </tr>
    </table>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript" src="https://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
    <script type="text/javascript">
        function AjaxFormSubmit() {
            var person = {};
            person.FirstName = $("#txtFirstName").val();
            person.LastName = $("#txtLastName").val();
            $.ajax({
                type: "POST",
                url: "/Home/AjaxMethod",
                data: JSON.stringify(person),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    if (response == true) {
                        alert("Form submitted!");
                    }
                    if (response == null) {
                        alert("Invalid Form data!");
                    }
                }
            });
        }
    </script>
</body>
</html>
 
 
Screenshot
ASP.Net MVC: Return NULL from ActionResult
 
 
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.

 
 
Downloads