Asp .Net MVC - SPLessons

ASP.Net MVC Actions

Home > Lesson > Chapter 6
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

ASP.Net MVC Actions

ASP.Net MVC  Actions

shape Introduction

In this article, the following topics will be discussed.

Action Methods

shape Description

Whole User Interaction in MVC depends upon the Controller's Action Method. A controller may have one or more action methods. Action Methods are mapped like one to one User Interaction(View). Action Method will be created within the class. The Action Method may be called in two ways.
  • By the browser request.
  • By the other Action methods within the controller.

Rules for the Action Methods

shape Rules

Following are the some situations which are not possible to do. [csharp] Private String Display() //Not Possible because Action Method must be public { } public static string display()//Not Possible because Action Method must not be static { } public void display()//Not Possible because Action Method must not be overloaded { } [/csharp]

shape Example

Create a simple application which prints and demonstrate about Action Methods.
  • Double click on the HomeController.cs which is under the Controller folder in the Solution Explorer.
  • Write the following code under the  HomeController.cs
[csharp] public string display() { return "Welcome to SPlessons";//Print this statement in the browser } [/csharp]
  • Run the Application and type the URL as shown in the below figure.

shape Output

Then, the output will display as follows.

shape Examples

Example to pass the values from the browser to Action Method. Write the following code in the HomeController.cs [csharp] public string display(string s) { return "Welcome to"+s;// displays the string along with this statement } [/csharp]
  • Run the application.Then, the browser will be displayed.
  • Type the below Url in the browser.

shape Output

Then, you will get a output as shown in the below figure.

shape Further

Now,Create another Action method like below. [csharp] public string Add(int a, int b) { return "Result of sum is" +(a+b);// displays the result along with this statement } [/csharp]
  • Type the below Url in the browser.
  • Then, you will get a output as shown in the below figure.

Non-Action Methods

shape Description

Non-Action Methods are used to implement any business logic code within the application. Non-Action Methods may be called in two ways
  • By the browser request.
  • By the other Non-Action methods within the controller.
Non-Action Methods can be overloaded and are same as Normal methods in C#. To create Non-Action Methods, precede the method with Non-Action Method Attribute like [NonAction].

shape Examples

Example Application on Non-Action Methods
  • Write the following code in the HomeController.cs
[csharp] [NonAction] public int Add(int a, int b) { return a + b; } public ActionResult Example() { int c = Add(50, 32); ViewData["Sum"] = "sum is" + c; return View(); } [/csharp]
  • Go to Example.cshtml and write the following code.
[html] @{ ViewBag.Title = "Example"; } <h3>Example for Non Action method</h3> <div> @ViewData["Sum"] </div> [/html]

shape Output

Run the application then you will get the following output.

Child Action Method

shape Description

  • Child Action Methods are used to work with the Partial Views.
  • Child Action Methods should be preceded with the ChildAction attribute like [ChildAction].
  • In general, one ChildAction method will map with only one Partial View.

What is Action Result?

shape Description

Action Results are nothing but Action Methods. But Action Results will have the return type as ActionResult. The above hierarchy shows that class derivations. The main class for all other classes is ActionResult. All the remaining classes which are shown above the ActionResult Class are derived classes of the ActionResult.

shape Syntax

The following code shows you the Signature for the ActionResult. public ActionResult Index()//ActionResult is the return type for this method { return view(); }

RedirectResult

shape Description

RedirectResult is used as a return type for Redirect method. The Types of Redirect Methods.

shape Syntax

Redirect to Action(String ActionName, String ControllerName)

shape Example

Create a new viewpage with the name Example. Write the following code under the Example.cshtml [html] <body> <div> <h2>Welcome to splessons</h2> </div> </body> [/html]
  • Go to Solution Explorer->Controllers->Double Click on the HomeController.cs
  • Write the following code under the HomeController.cs
[csharp] public RedirectToRouteResult Example() { return RedirectToAction("Example","Home"); } [/csharp]
  • Press f5 to run the application. Then, the output will be appear as shown in the below figure.

shape Output

shape Syntax

Redirect(string url)

shape Examples

Example Application Requirement: Create Hyper Links in a view page using HTMLHelperClass that redirects to different websites.
  • Create a new view page with the name Example2.
  • Write the following code in the Example2.cshtml
[html] <h2>Welcome to SPlessons</h2> <div> @Html.ActionLink("Gmail","Gmail","Home") @Html.ActionLink("Google","Google","Home") @Html.ActionLink("Splessons","Splessons","Home") </div> [/html]
  • Go to Solution Explorer->Controllers->Double Click on the HomeController.cs
  • Write the following code under the HomeController.cs
[csharp] public ActionResult Example() { return view(); } public RedirectResult Gmail() { return Redirect("http://www.gmail.com"); } public RedirectResult Google() { return Redirect("http://www.google.com"); } public RedirectResult Splessons() { return Redirect("http://www.splessons.com"); } [/csharp]
  • Press f5 to run the application. Then, the output will appear as shown in the below figure.

shape Output

  • Click on the links. Then, the related links will be open.
File Result File Result is used to return the required file data to the browser. File Result will work with the following method. File Method

shape Syntax

File(string Filename, string Contexttype)

shape Example

Create a new viewpage with the name Example3. Write the following code in the Example3.cshtml [csharp] public FileResult Example3 { return File(Server.MapPath(~/Views/image.jpg","image.jpg"); //copies the image.jpg into the Views folder. } [/csharp]

Filters

shape Description

Filters are used to implement pre-action or post-action behavior within the controller. Filters will encapsulate the result of an ActionMethod and is used to perform FrameWork level operations on behalf of the ActionMethod. Filters can be applied at Action Method level or at the controller level or at the application level. MVC will support 4 types of filters.