How to: Add a View to an MVC Application in Visual Studio
Introduction
In ASP.NET MVC, views (sometimes referred to as view pages) represent the UI at various states of the application. As a user interacts with a view, data is routed from that view to a controller action method, which in turn might render another view. An MVC application might have several controllers, each of which might contain multiple action methods; each action might render a different view. By convention, the views are organized in folders that are named after the associated controller.
This topic shows you how to add a view to an MVC application. It also shows you how to add a folder to contain the views if you need to.
To add a Views folder to the project
-
In Solution Explorer, right-click the Views folder, click Add, and then click New Folder.
-
Rename the folder based on the name of the controller.
Note
For an MVC application, Visual Studio requires the views for each controller to be grouped in a folder that is named after the controller. For example, the views for HomeController are grouped in the Views/Home folder, and the views for LoginController are grouped in the Views/Login folder.
To add a view to an MVC application
-
In Solution Explorer, expand the Views folder and right-click the folder named after the controller that will render the new view. For example, if HomeController will render the view, right-click the Home folder.
-
Click Add, and then click New Item.
-
Under Categories, expand either Visual Basic or Visual C# (depending on your project), expand Web, and then click MVC.
-
Under Templates, select either MVC View Page (no master page) or MVC View Content Page (associated with a master page).
-
In the Name box, type the name of the view.
Note
By convention, the name of the view is assumed to be the same as the name of the action method. For example, if the action method is named Login, name the associated view Login.aspx.
-
Click Add.
If you selected MVC View Page, the new view is generated together with its code-behind file and you are finished.
If you selected MVC View Content Page, The Select a Master Page dialog box is displayed.
-
Under Project folders, under Views, select Shared.
-
Under Contents of folder, click the master page (.Master) file, and then click OK.
The new view is generated together with its code-behind 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.