All
Types Of Action Methods In ASP.NET MVC 5
Step-1
Create Controller [ StudentController ]
Step-2
Create View Page [ Empty without Model ]
ViewResult - Represents HTML
and markup.
Controller
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebApplication1.Controllers
{
public class StudentController : Controller
{
public ViewResult Index()
{
ViewBag.ItemList = "Computer
Shop Item List Page";
return View();
}
}
}
Index.cshtml (View Page Code)
@{
Layout = null;
}
<!DOCTYPE html>
<html><body> @ViewBag.ItemList </body> </html>
EmptyResult - Represents no
result.
Note : Only change controller . Index.cshtml
code as it is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebApplication1.Controllers
{
public class StudentController : Controller
{
public EmptyResult Index()
{
ViewBag.ItemList = "Computer
Shop Item List Page";
return new EmptyResult();
}
}
}
Output: It will return a blank page with no result.
RedirectResult - Represents a redirection to a new URL.
Controller Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebApplication1.Controllers
{
public class StudentController : Controller
{
public RedirectResult Index()
{
//return
Redirect("Home/Contact");
return
Redirect("http://nileshsir.com");
}
}
}
JsonResult - Represents a JavaScript Object
Notation result that can be used in an AJAX application.
JSON Result returns simple text file
format and key value pairs. Sometimes you may want to return data in JSON
Format and that situation you JSONResult is the best option.
Controller Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebApplication1.Controllers
{
public class StudentController : Controller
{
public JsonResult Index()
{
Employee emp = new Employee() //
class create inside controller
{
ID = "Emp23",
Name = "Steven
Clark",
Mobile = "825415426"
};
return
Json(emp, JsonRequestBehavior.AllowGet);
}
public class Employee
{
public string ID { get; set; }
public string Name { get; set; }
public string Mobile {
get; set; }
}
}
}
Output :
{"ID":"Emp23","Name":"Steven Clark","Mobile":"825415426"}
JavaScriptResult - Represents a
JavaScript script.
It
returns java script that can be executed on the client browser. It sends
javascript content in response to browser. This block allow you to execute java
script on client at run time.
Controller Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebApplication1.Controllers
{
public class StudentController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpGet]
public JavaScriptResult
WarningMessage()
{
var msg = "alert('Are
you sure want to Continue?');";
return new JavaScriptResult(){ Script = msg
};
}
}
}
Index.cshtml Code
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script>
$(document).ready(function () {
$("button").click(function () {
$.getScript("/Student/WarningMessage");
});
});
</script>
</head>
<body>
<div>
@ViewBag.ItemList
</div>
<button>Show
Message</button>
</body>
</html>
ContentResult - Represents a text
result.
It returns user-defined content
type. It is useful when you want to send some plain text message to browser
screen.
Controller Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebApplication1.Controllers
{
public class StudentController : Controller
{
public ContentResult Index()
{
return Content("Hello
ASP.NET MVC 5");
}
}
}
INDEX.cshtml
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
@ViewBag.ItemList
</div>
</body>
</html>
FileResult - Represents a downloadable file (with the binary
content).
It represents the content of the
file.
Controller Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebApplication1.Controllers
{
public class StudentController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpGet]
public FileResult Download()
{
byte[] fileBytes =
System.IO.File.ReadAllBytes(@"D:\testdata\demo.txt");
string filename = "demo.txt";
return
File(fileBytes, filename); // direct open file data
return
File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, filename);
//download file in browser
}
}
}
Index.cshtml code
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
@ViewBag.ItemList
@Html.ActionLink("Download
Text File", "Download", "Student")
</div>
</body>
</html>
PartialViewResult
Step 1: Add a partial
page. Go to Solution Explorer Shared. Right-click on
it and select Add View.
Step 2: Create
partial view page as described in this picture.
Step 3: Add the following
code in message.cshtml
page.
<h1> This is
PartialViewResult Example Output. </h1>
Step 4 : Controller Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebApplication1.Controllers
{
public class StudentController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpGet]
public PartialViewResult
messagepage()
{
return
PartialView("message");
}
}
}
Step -5
Index.cshtml code
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
@ViewBag.ItemList
@{
Html.RenderAction("messagepage", "Student");
}
</div>
</body>
</html>