MVC5 controller, view simple description

Source: Internet
Author: User

This series will be more detailed than MVC4. Mvc4 record or not recorded things will also be mentioned here.

Controller

Automatic assembly:

General automatic Assembly is useful when adding

View:

The view returned by the controller is actually some static HTML. The dynamic is not good, transfer a small amount of data from the controller, the simplest use VIEWBAG. Applies only to small amounts of data.

Strongly typed views

Start learning when you think ViewBag is OK, one to write the project when you will feel viewbag very inconvenient.

ViewBag of the poor

Because ViewBag is dynamically parsed, it needs to go down every time it's used.

Upgrade a little, but this time you can not take the output properties, because it is dynamic parsing, as long as the runtime will be resolved, will be error . Dynamic=var

At this time, we need a strong type to improve the above deficiencies .

Controller to view value: The difference between ViewBag and ViewData

ViewData is more like a special dictionary type.

The VIEWBAG is dynamically parsed.

View Model

Add a View

Razor View Engine

Code expression

The core conversion character in Razor is @, and razor is smart to make it very popular. Smart: Most of the characters can be judged by their own properties or displayed.

HTML encoding avoids XSS attacks, which are detailed in later articles

Razor Syntax Examples

Layout is equivalent to the master page in Web Forms

① Create a new MVC5 layout page sitelayoutpage.cshtml layout page can be thought of as a special view.

② Create a view associated with it

Index page We made a good understanding of the correlation, the test page we did not do the correlation can also be related. That's because there's a _viewstart.cshtml page .

Declare the footer section in index only and no longer declare it in test

Open test and you'll get an error.

Specify partial views

① Create a method that returns a partial view

② adding views

Partial views are generally used to make local updates very useful. It is very common to put HTML into a local view and then use Ajax for updates.

Index page:

Layout page:

MVC5 controller, view simple description

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.