How to: Add an Action Method to an MVC Controller in Visual Studio
Introduction
A controller includes action methods that it can call in response to user interactions, such as clicking a link or submitting a form. Action methods have the following requirements:
-
An action method must return an ActionResult object. You do not have to instantiate an ActionResult object. That is performed for you by the action helper.
-
An action method must call one of the following action helpers as the return value, which are defined in the Controller class: View, Redirect, RedirectToAction, RedirectToRoute, Content, or Json.
To add an action method to an MVC controller
-
Open the controller class that you want to add the new action method to.
-
Add the following code, which defines an empty action method:
Public Function MyAction()
' Enter logic here.
Return View()
End Function
[C#]
public ActionResult MyAction()
{
// Enter logic here.
return View();
}
-
Enter the logic for the action method.
-
Save and close the file.
This topic is ASP.NET Extensions documentation and is unsupported by Microsoft. Blank topics are included as placeholders and existing content is subject to change in future releases.