1. Basic rules
The responsibility of the view is to provide the user interface to the user.
The view is located under the View directory: There are common views that require controller rendering, a partial view, and a variety of views, such as layout views.
2. View Rendering
By default, the controller renders a view with the same name as the action in a directory with the same name as the controller, or in action, by return view (view name) to specify additional views that need to be rendered, if the view is in the same directory, with the view name, If the view is in a different directory, you need to provide the server root path "~/view/example/index.cshtml".
3. Strongly-typed views and data transfer
The controller passes data to the rendered view through ViewData and ViewBag,ViewData is the object of the key-value pair, and ViewBag is the dynamic field type . ViewData is a viewdatadictionary type, not a normal dictionary type, and it has an extra model property that makes it easy to pass a particular view type to the view. Called the View Master model (only one), this object needs to be used as a return view ( object ) and is declared with the following code at the top of the view:
Example: Strongly typed mode
@model list<mvcmusicstore.models.album>
@foreach (Var album in Model)
Gets the declared strongly typed object in the view by @Model variable.
If you want to pass other data that is not related to the View master model, you can store it in ViewBag and implement a strongly typed effect by type conversion.
Example: non-strongly typed mode
@using Mvcmusicstore.models;
@foreach (Album Album in Viewbag.albums)
4. View Model ViewModel
For a view to have only one view of the main model of the problem, in order to be able to use all the data in a strongly typed way to access, you can write a separate "View model class", you need to pass the data defined in the View model class.
Example: Defining a View model
public class Shoppingcartviewmodel
{
Public list<cart> Cartitems {get; set;}
Public decimal Carttotal {get; set;}
}
Strongly typed reference view model: @model MvcMusicStore.ViewModels.ShoppingCartViewModel
ASP. NET MVC 4 Advanced Programming Learning Notes: Chapter III view