知无涯

吾生也有涯,而知也无涯,以有涯随无涯,殆已
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

Posted on 2008-08-01 15:55  Fred du  阅读(160)  评论(0编辑  收藏  举报

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

  1. Open the controller class that you want to add the new action method to.

  2. Add the following code, which defines an empty action method:


    [C#]
    public ActionResult MyAction()
    {
        // Enter logic here.
        return View();
    }
    
  3. Enter the logic for the action method.

  4. Save and close the file.


Technorati : asp.net mvc