In this article, the following topics will be discussed.
Action Methods
Rules for the Action Methods
Example Application on Action Methods
Non-Action Methods
Example Application on Non-Action Methods
Child Action Method
Action Result
RedirectResult
File Result
Filter
Action Methods
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
Rules
Action Methods must be public.
Action Methods cannot be static.
Action Methods cannot be overloaded.
Action Methods cannot have Unbounded Generic Type Parameters.
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]
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.
Output
Then, the output will display as follows.
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.
URL
http://localhost:7867/home/display?s=SPLessons
Output
Then, you will get a output as shown in the below figure.
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.
URL
http://localhost:7867/home/Add?a=10&b=20
Then, you will get a output as shown in the below figure.
Note
Here,Query String is used to pass the data from the browser to the action method.
Non-Action Methods
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].
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]
Output
Run the application then you will get the following output.
Child Action Method
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?
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.
Syntax
The following code shows you the Signature for the ActionResult.
public ActionResult Index()//ActionResult is the return type for this method
{
return view();
}
Note
All Action Results are Action Methods.But all Action Methods are not Action Results.
RedirectResult
Description
RedirectResult is used as a return type for Redirect method.
The Types of Redirect Methods.
Redirect to Action
Redirectt to Action Perminent
Redirect to Route
Redirect to Route Perminent
Syntax
Redirect to Action(String ActionName, String ControllerName)
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]
Note
In the above code, RedirectToRouteResult is the return type for the method RedirecttoAction
Press f5 to run the application. Then, the output will be appear as shown in the below figure.
Output
Syntax
Redirect(string url)
Examples
Example ApplicationRequirement: 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.
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
Syntax
File(string Filename, string Contexttype)
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
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.