In this article I will explain with an example, how to create a VOID ActionResult (Action Method) i.e. an ActionResult (Action Method) inside Controller that does not return View in ASP.Net MVC Razor.
There is nothing like a VOID ActionResult (Action Method), hence ASP.Net MVC has created a class EmptyResult whose object is returned in case when NULL value or Nothing needs to be returned from Controller to View in ASP.Net MVC Razor.
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 an EmptyResult object (NULL) is 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 new EmptyResult();
}
}
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
Browser Compatibility
The above code has been tested in the following browsers.
* All browser logos displayed above are property of their respective owners.
Downloads