{"id":4893,"date":"2024-02-06T12:17:22","date_gmt":"2024-02-06T12:17:22","guid":{"rendered":"https:\/\/isophal.com\/?p=4893"},"modified":"2024-02-06T12:17:23","modified_gmt":"2024-02-06T12:17:23","slug":"all-types-of-action-methods-in-asp-net-mvc-5","status":"publish","type":"post","link":"https:\/\/isophal.com\/news\/2024\/02\/06\/4893.html\/","title":{"rendered":"All Types Of Action Methods In ASP.NET MVC 5"},"content":{"rendered":"\n<p><em><strong>In this chapter, you will learn:<\/strong><br><strong>1.<\/strong>&nbsp;What is Action Methods in ASP.NET MVC 5?<br><strong>2.<\/strong>&nbsp;Uses of Action Methods<br><strong>3.<\/strong>&nbsp;How many types of Action Methods in MVC?<br><strong>4.<\/strong>&nbsp;Programming Example<\/em><\/p>\n\n\n\n<p>In the last chapter, you have seen an ActionResult method that returns Index() view page to the user. In this chapter, we will clear the concept of Action Methods in MVC.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">WHAT IS ACTION METHOD IN ASP.NET MVC 5?<\/h3>\n\n\n\n<p>All the public methods which are written inside a Controller are known as Action Method. When creating Action Method you must follow these rules.<\/p>\n\n\n\n<p><strong>a.<\/strong>&nbsp;Action method must be public.<br><strong>b.<\/strong>&nbsp;It cannot be overloaded.<br><strong>c.<\/strong>&nbsp;It cannot be a static method.<br><strong>d.<\/strong>&nbsp;Every controller has at least one default Action method Index() that returns the view page.<br><strong>e.<\/strong>&nbsp;ActionResult is a base class of all the result type action methods.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">TYPES OF ACTION METHOD<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/09\/2.jpg\"><img decoding=\"async\" src=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/09\/2.jpg\" alt=\"Action Result\"\/><\/a><\/figure>\n\n\n\n<p><code>ActionResult<\/code>&nbsp;is the base class of all the result type action method. There are following Result type action method in MVC.<strong>ViewResult<\/strong>&nbsp;&#8211; Represents HTML and markup.<br><strong>EmptyResult<\/strong>&nbsp;&#8211; Represents no result.<br><strong>RedirectResult<\/strong>&nbsp;&#8211; Represents a redirection to a new URL.<br><strong>JsonResult<\/strong>&nbsp;&#8211; Represents a JavaScript Object Notation result that can be used in an AJAX application.<br><strong>JavaScriptResult<\/strong>&nbsp;&#8211; Represents a JavaScript script.<br><strong>ContentResult<\/strong>&nbsp;&#8211; Represents a text result.<br><strong>FileContentResult<\/strong>&nbsp;&#8211; Represents a downloadable file (with the binary content).<br><strong>FilePathResult<\/strong>&nbsp;&#8211; Represents a downloadable file (with a path).<br><strong>FileStreamResult<\/strong>&nbsp;&#8211; Represents a downloadable file (with a file stream).<\/p>\n\n\n\n<p><strong>Programming Example<\/strong><\/p>\n\n\n\n<p>1. ViewResult<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\/\/ GET: Item<\/li>\n\n\n\n<li>public ViewResult Index()<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>ViewBag.ItemList = &#8220;Computer Shop Item List Page&#8221;;<\/li>\n\n\n\n<li>return View();<\/li>\n\n\n\n<li>}<\/li>\n<\/ol>\n\n\n\n<p><strong>Output:<\/strong>&nbsp;It will return Index View Page<\/p>\n\n\n\n<p>EmptyResult<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\/\/GET: Item<\/li>\n\n\n\n<li>public EmptyResult Index()<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>ViewBag.ItemList = &#8220;Computer Shop Item List Page&#8221;;<\/li>\n\n\n\n<li>return new EmptyResult();<\/li>\n\n\n\n<li>}<\/li>\n<\/ol>\n\n\n\n<p><strong>Output:<\/strong>&nbsp;It will return a blank page with no result.<\/p>\n\n\n\n<p>RedirectResultRedirect Results is used for returning results to specific url. When you need to redirect to another action method, you can use RedirectResult Action Method.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>public RedirectResult Index()<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>return Redirect(&#8220;Home\/Contact&#8221;);<\/li>\n\n\n\n<li>}<\/li>\n<\/ol>\n\n\n\n<p>When you will run this code, It will redirect you to&nbsp;<strong>Contact<\/strong>&nbsp;Page.<\/p>\n\n\n\n<p>JSONResult<strong>JSON<\/strong>&nbsp;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.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>public JsonResult Index()<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>Employee emp = new Employee()<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>ID = &#8220;Emp23&#8221;,<\/li>\n\n\n\n<li>Name = &#8220;Steven Clark&#8221;,<\/li>\n\n\n\n<li>Mobile = &#8220;825415426&#8221;<\/li>\n\n\n\n<li>};<\/li>\n\n\n\n<li>return Json(emp, JsonRequestBehavior.AllowGet);<\/li>\n\n\n\n<li>}<\/li>\n\n\n\n<li>public class Employee<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>public string ID { get; set; }<\/li>\n\n\n\n<li>public string Name { get; set; }<\/li>\n\n\n\n<li>public string Mobile { get; set; }<\/li>\n\n\n\n<li>}<\/li>\n<\/ol>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/09\/New-Picture-1.png\"><img decoding=\"async\" src=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/09\/New-Picture-1.png\" alt=\"JSON Output\"\/><\/a><\/figure>\n\n\n\n<p>JavaScriptResult<\/p>\n\n\n\n<p>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.<strong>Example:<\/strong><br><strong>Step 1:<\/strong>&nbsp;Add this&nbsp;<code>JavaScriptResult()<\/code>&nbsp;method in home controller.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>[HttpGet]<\/li>\n\n\n\n<li>public JavaScriptResult WarningMessage()<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>var msg = &#8220;alert(&#8216;Are you sure want to Continue?&#8217;);&#8221;;<\/li>\n\n\n\n<li>return new JavaScriptResult() { Script = msg };<\/li>\n\n\n\n<li>}<\/li>\n<\/ol>\n\n\n\n<p><strong>Step 2:<\/strong>&nbsp;Open&nbsp;<code>Index.cshtml<\/code>&nbsp;and add the following highlighted code.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>@{<\/li>\n\n\n\n<li>ViewBag.Title = &#8220;Computer Shop Management&#8221;;<\/li>\n\n\n\n<li>}<\/li>\n\n\n\n<li>&lt;script src=&#8221;~\/Scripts\/jquery-1.10.2.js&#8221;&gt;&lt;\/script&gt;<\/li>\n\n\n\n<li>&lt;div class=&#8221;jumbotron&#8221;&gt;<\/li>\n\n\n\n<li>&lt;h2 style=&#8221;color:chocolate&#8221;&gt;Welcome to Computer Shop Management&lt;\/h2&gt;<\/li>\n\n\n\n<li>&lt;\/div&gt;<\/li>\n\n\n\n<li>&nbsp;<\/li>\n\n\n\n<li>&nbsp;<\/li>\n\n\n\n<li>&lt;script&gt;<\/li>\n\n\n\n<li>$(document).ready(function () {<\/li>\n\n\n\n<li>$(&#8220;button&#8221;).click(function () {<\/li>\n\n\n\n<li>$.getScript(&#8220;\/Home\/WarningMessage&#8221;);<\/li>\n\n\n\n<li>});<\/li>\n\n\n\n<li>});<\/li>\n\n\n\n<li>&lt;\/script&gt;<\/li>\n\n\n\n<li>&nbsp;<\/li>\n\n\n\n<li>&lt;button&gt;Show Message&lt;\/button&gt;<\/li>\n<\/ol>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/09\/New-Picture-1-1.png\"><img decoding=\"async\" src=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/09\/New-Picture-1-1.png\" alt=\"Java Script Output\"\/><\/a><\/figure>\n\n\n\n<p>Content ResultIt returns user-defined content type. It is useful when you want to send some plain text message to browser screen.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>public ContentResult Index()<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>return Content(&#8220;Hello ASP.NET MVC 5&#8221;, &#8220;text\/plain&#8221;, System.Text.Encoding.UTF8);<\/li>\n\n\n\n<li>}<\/li>\n<\/ol>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/09\/New-Picture-2-2.png\"><img decoding=\"async\" src=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/09\/New-Picture-2-2.png\" alt=\"Content Result Output\"\/><\/a><\/figure>\n\n\n\n<p>File ResultIt represents the content of the file.<br><strong>Step 1:<\/strong>&nbsp;Add following code in HomeControllers<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>[HttpGet]<\/li>\n\n\n\n<li>public FileResult Download()<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>byte[] fileBytes = System.IO.File.ReadAllBytes(@&#8221;D:\\folder\\myfile.txt&#8221;);<\/li>\n\n\n\n<li>string filename = &#8220;myfile.txt&#8221;;<\/li>\n\n\n\n<li>return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, filename);<\/li>\n\n\n\n<li>}<\/li>\n<\/ol>\n\n\n\n<p><strong>Step 2:<\/strong>&nbsp;Add Action Link in&nbsp;<code>Index.cshtml<\/code>&nbsp;page.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>@Html.ActionLink(&#8220;Download Text File&#8221;,&#8221;Download&#8221;,&#8221;Home&#8221;)<\/li>\n<\/ol>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/09\/New-Picture-3-2.png\"><img decoding=\"async\" src=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/09\/New-Picture-3-2.png\" alt=\"Output File Result\"\/><\/a><\/figure>\n\n\n\n<p>PartialViewResultReturns HTML from Partial view.<\/p>\n\n\n\n<p><strong>Step 1:<\/strong>&nbsp;Add a partial page. Go to&nbsp;<strong>Solution Explorer<\/strong>&nbsp;<strong>Shared<\/strong>.&nbsp;<strong>Right-click<\/strong>&nbsp;on it and select&nbsp;<strong>Add<\/strong>&nbsp;<strong>View<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/09\/New-Picture-4-1.png\"><img decoding=\"async\" src=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/09\/New-Picture-4-1.png\" alt=\"Add View\"\/><\/a><\/figure>\n\n\n\n<p><strong>Step 2:<\/strong>&nbsp;Create partial view page as described in this picture.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/09\/New-Picture-5-3.png\"><img decoding=\"async\" src=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/09\/New-Picture-5-3.png\" alt=\"Add View Template\"\/><\/a><\/figure>\n\n\n\n<p><strong>Step 3:<\/strong>&nbsp;Add the following code in&nbsp;<code>message.cshtml<\/code>&nbsp;page.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">This is&nbsp;<strong>PartialViewResult<\/strong>&nbsp;Example Output.<\/h2>\n\n\n\n<p><strong>Step 4:<\/strong>&nbsp;Open&nbsp;<code>HomeController.cs<\/code>&nbsp;and add following line of code.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>[HttpGet]<\/li>\n\n\n\n<li>public PartialViewResult messagepage()<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>return PartialView(&#8220;message&#8221;);<\/li>\n\n\n\n<li>}<\/li>\n<\/ol>\n\n\n\n<p><strong>Step 5:<\/strong>&nbsp;Go to&nbsp;<code>Index.cshtml<\/code>&nbsp;and add this code.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>@{<\/li>\n\n\n\n<li>Html.RenderAction(&#8220;messagepage&#8221;, &#8220;Home&#8221;);<\/li>\n\n\n\n<li>}<\/li>\n<\/ol>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/09\/New-Picture-6-3.png\"><img decoding=\"async\" src=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/09\/New-Picture-6-3.png\" alt=\"Output\"\/><\/a><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">SUMMARY<\/h3>\n\n\n\n<p>In this chapter, you learned several types of Action Methods. In the next chapter, you will learn about&nbsp;<a href=\"https:\/\/www.completecsharptutorial.com\/asp-net-mvc5\/understanding-views-basics-in-mvc-5.php\">Views in ASP.NET MVC 5<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this chapter, you will learn:1.&nbsp;What is&hellip;<\/p>\n","protected":false},"author":1,"featured_media":4894,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[189,188,140],"tags":[191],"class_list":["post-4893","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-asp-net","category-c-sharp","category-programming","tag-asp-net"],"_links":{"self":[{"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/posts\/4893","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/comments?post=4893"}],"version-history":[{"count":1,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/posts\/4893\/revisions"}],"predecessor-version":[{"id":4895,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/posts\/4893\/revisions\/4895"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/media\/4894"}],"wp:attachment":[{"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/media?parent=4893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/categories?post=4893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/tags?post=4893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}