Virtual Classes Logo          Virtual Classes
    Read Anytime,Anywhere


Controller in MVC handles user interaction. Controllers read data from a view, control user input, and send input data to the model. When view request the data or post data to controller,action method do the processing on data and send response back to view.

To add a Controller to your MVC application.
1-Goto to solution explorer --> Right Click on Controllers folder-->Choose Add-->Choose Controller
Belog image shows adding a Controller to a MVC application.
Adding a Controller In MVC

2-A dialog box will be opened. Give a name to the Controller and Click on "Add" button.
While creating a Controller you can choose scaffolding options. As you can see in below image,Controller name must have controller key word in the last of its name. So while giving a name to controller you can replace only highlighted word,in image it's Default1.

Controller In MVC

3-Add Action Method to your Controller.
Action method hold the processing logic. When any page is requested in MVC, it's Action methods which handles the request. Action method do the processing and send response back to browser.
Action method In Controller

Commonly used Action Methods return types-
1-ActionResult-: Base Class of all other Action Method return types.
2-ViewResult-: Renders a view as a web page.
3-PartialViewResult-: Use this return type if you want to render a partial view.
4-RedirectToRouteResult-: Redirect to another action method.
5-ContentResult-: Returns a user-defined content type.
6-JsonResult-: Retuns a serialized JSON object.
7-FileResult-: Use this return type if you want to return a file result(binary output) to write to the response .