ASP. NET MVC 4 Advanced Programming Learning Notes: Chapter III view

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.