Quicktostudy
Contact Us| About Us| Terms| Privacy Policies
Dot Net MVC Tutorial

MVC View

MVC View

The MVC view is responsible for providing the user interface (UI) to the user. After the controller has executed the appropriate logic for the requested URL, it delegates the display to the view.

Each controller folder contains a view file for each action method, and the file is named the same as the action method.

Adding a MVC View

The ASP.NET MVC tooling for Visual Studio makes adding a view using the Add View dialog very easy.

The easiest way to display the Add View dialog is to right-click in an action method. You can use any action method you'd like; for this example you can just add a new action method named Edit and then create a view for that action using the Add View dialog. Begin by adding an Edit action method to the HomeController in an MVC 5 application that contains the following code:

  public ActionResult Edit()
  {
  	return View();
  }
  

Next, launch the Add View dialog by right-clicking an action method and selecting Add View

MVC View

This brings up the Add View dialog, as shown below

MVC View

The following list describes each menu item in detail:
View name: When launching this dialog from the context of an action method, the view name is prepopulated using the name of the action method. Naturally, the view name is required.
Template: After you select a type, you can also choose a scaffold template. These templates use the Visual Studio templating system to generate a view based on the model type selected.

MVC View

MVC View Scaffold Types

  • Create : Creates a view with a form for generating new instances of the model. Generates a label and input field for each property of the model type.
  • Delete : Creates a view with a form for deleting existing instances of the model. Displays a label and the current value for each property of the model.
  • Details : Creates a view that displays a label and the value for each property of the model type.
  • Edit : Creates a view with a form for editing existing instances of the model. Generates a label and input fi eld for each property of the model type.
  • Empty : Creates an empty view. Only the model type is specified using the @model syntax.
  • Empty (without model) : Creates an empty view, as with the Empty scaffold. In this case, however, there are no model so you are not required to select a model type when you select this scaffold. This is the only scaffold type which does not require you to select a model type.
  • List : Creates a view with a table of model instances. Generates a column for each property of the model type. Make sure to pass an IEnumerable to this view from your action method. The view also contains links to actions for performing the create/edit/delete operations.

Create as a partial view: Selecting this option indicates that the view you will create is not a full view, thus the Layout option is disabled. The resulting partial view looks much like a regular view, except you will have no html tag or head tag at the top of the view.

Use a layout page: This option determines whether or not the view you are creating references a layout or is a fully self-contained view. Specifying a layout is not necessary if you choose to use the default layout because the layout is already specified in the _ViewStart .cshtml file. However, you can use this option to override the default Layout file.

Home About method—HomeController.cs

  public ActionResult About()
  {
  	ViewBag.Message = "Your application description page.";
  	return View();
  }
  

Home About view—About.cshtml

  @{
  ViewBag.Title = "About";
  }
  @ViewBag.Title.
  @ViewBag.Message
  Use this area to provide additional information.
  
MVC View
Contact Us| About Us| Terms| Privacy Policies
Powered by Lorquins Technologies© 2017 QuickToStudy.com. All Rights Reserved