Lecture 3rd: view layer usage skills in MVC (Part 1)

Source: Internet
Author: User
Tags visual studio 2010

2010.9.3 su Peng

Content

-What is the view layer?

-Use a strong type in the definition

-Use custom viewmodels

-Htmlhelper class

 

Prerequisites

-Install Visual Studio 2010 Express

-Understand ASP. NET

-Understand the basic concepts of the Design Mode

 

What is the view layer?

-Viewdatadictionary class

It is the core of the view layer. It generates HTML based on the query results after receiving user requests. The requested data is generally a data entity from the model layer.

The view layer uses the formatted data as a container to transmit data between the Controller and view. Without strong names, the view layer is just a simple dictionary object, that is, the viewdatadictionary class.

 

Common View styles

It can be seen that the view style is similar to webform, but they are also different.

First, the view page inherits from system. Web. MVC, while the webform inherits from system. Web. form.

Second, viewdata is used to display view data. In addition, <%: the following content indicates the object of the MVC view to be called.

Third, view does not have the form runat = server mark. If you want to add this flag to the view page, call the Server Control and compile the code, but it may not run because it does not have viewstate or PostBack. Viewstate is the basis of control. Without viewstate, the state of control cannot be recorded, and the server cannot interact with the foreground. PostBack is the soul of control. to interact with the server, control must have a PostBack mechanism. Unfortunately, because the page inherits from system. web. MVC, which does not contain these two functions.

In addition, it is not recommended to mark form runat = server on the page for two reasons:

First, this broke the MVC Architecture, and the MVC Architecture Design Concept was completely overturned. Originally, the MVC design cost a lot of effort through the hierarchical structure of folders, routing, and engine, in order to completely separate the presentation layer from the business logic layer. As you can imagine, the logic processing of MVC cannot be processed in view, but in controller. Therefore, if you add a server-side control to the viewCodePage, how to save the data in the front code page, so that the encapsulation is completely broken. The previous hard work was in vain.

Second, there is no viewstate or PostBack.

 

View Obfuscation

-View outputs data rather than styles.

 

No matter the orchestration code, the view layer only has data and no style. Data indicates that the displayed data records are the same as the structure in the database, and the style adds some table tags on them.

Never output HTML styles in the controller.

Try not to write HTML styles in the view by yourself, and try to use the htmlhelper class that comes with MVC.

View should contain as few code as possible because it cannot be tested in units. The existing performance Layer tests are all black box tests.

 

Specify View

-Specify view as output

 

Action first goes to the home folder of view (default). If the file name cannot be found, it will continue to find the file in the share folder. If the file cannot be found, an error will be reported.

This viewdata is not for routing, but global, similar to session.

If the view to be switched is not in home and share, you must clearly specify the file path and file suffix.

 

Use a strong type in view

-Use strong data types in view

 

In the previous example, viewdata is of a weak type and functions similar to session.

Non-Strong writing method (not recommended)

-Controller & view

Strongly typed writing (recommended)

-Controller & view

 

Viewmodels

-Custom viewmodels

 

Htmlhelper class

-Why do we need it?

 

ASP. net mvc is the most widely used saying that ASP. net mvc can completely control its style. It uses the htmlhelper class to help us do these things. It actually includes some suitable outerwear on viewdata so that you can do some processing, this includes outputting its content as needed into the HTML control you want. In this way, you can easily integrate your own information in the application, without worrying about whether HTML is appropriate in the future, and htmlhelper can be expanded and customized by yourself, therefore, it can fully satisfy the content you want to output.

 

Htmlhelper class and Extension Method

-Viewpage provides an htmlhelper attribute named HTML.

-Using system. Web. MVC. html namespace

 

Strong type in HTML

-Mvc1 <%: HTML. textboxfor ("propertyname") %>

-MVC2 <%: HTML. textboxfor (M => M. propertyname) %>

 

Html. encode

-Equivalent to <%: %>

 

Translate the HTML tag to be output into other information, and then output this method that makes sure it looks consistent with the HTML language.

 

Html. textbox

-<%: HTML. Textbox ("name") %>

-Equivalent

-<Input id = "name" name = "name" type = "text" value = ""/>

 

Weak type

Controller

View

Result html

 

Strong type

Controller

View

Result html

 

Strong-type IDs replace dots with underscores because JavaScript has a special understanding of vertices and requires a large number of IDs in our applications. These IDS need to be parsed by JavaScript and some operations need to be implemented, therefore, the system automatically converts it to an underscore.

 

Use a strong Controller

View

Result html

 

Textboxfor

 

 

That is, the strong type of the Nanda expression. He accepts three parameters. The first parameter is the object found by the Nanda expression, the second parameter is to specify the view, and the third parameter is the class ID, supports direct output with style.

2010.9.21

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.