Поделиться через


How to: Add an Action Method to an MVC Controller in Visual Studio

A controller includes action methods that it can call in response to user interactions, such as clicking a link or submitting a form. Most action methods 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 its return value:

To add an action method to an MVC controller

  1. In Visual Studio, open the controller class that you want to add the action method to.

  2. Add code similar to the following, which defines an action method that renders a view named MyAction.

    Public Function MyAction()
        ' Enter logic here.
        Return View()
    End Function
    
    public ActionResult MyAction()
    {
        // Enter logic here.
        return View();
    }
    
  3. Enter the logic for the action method.

  4. Save and close the file.

See Also

Tasks

Walkthrough: Creating a Basic MVC Project with Unit Tests in Visual Studio

How to: Add a Controller to an MVC Application in Visual Studio

Concepts

Controllers and Action Methods in MVC Applications