View of ASP. net mvc Framework (5)

Source: Internet
Author: User
Tags actionlink

I. Basic concepts:

1. Although code assembly HTML can be written in the Action method and returned to the client through the response. Write () method, this method is not desirable. A better way is to process business and data logic only in the Action method, and pass the relevant data to a separate view template. The view is responsible for generating HTML display. Later, we will see that the view template is a simple text file that usually contains HTML identifiers and embedded scripts.

2. You can use (. aspx), master page (. Master), and user control (. ascx) as views ).

3. Place the agreed view under the views directory. The Controller first looks for the views/{controllername} directory. If the directory is not found, the Controller continues to look for the views/shared/directory. If the directory is still found, an error message is returned.

4. When the view template and the Action method called by the Controller have the same name, developers do not have to explicitly specify the view Template Name. We only need to pass the model object to the view () auxiliary method (no view name is required), Asp. net MVC will automatically deduce the view we use/views/[controllername]/[actionname] and display the result. In this way, we can simplify some code to avoid repeating the view name in the code.

Ii. View ):

Use the view template to return html. We call the view () method of the controller base class to return the viewresult object.

Public actionresult details (int id ){
Dinner guestbook = guestbookrepository. getdinner (ID); // return a guestbook object
If (guestbook = NULL)
Return view ("notfound ");
Else
Return view ("details", guestbook );
}

In the code above, the prototype of the view () auxiliary method we use is as follows:

The first parameter of the view () auxiliary method is the view template file, which is used to generate HTML responses. The second parameter is the model object, which contains the data required for generating HTML responses from the view template.
In the details () Action method, we use the id value to retrieve a specific guestbook object. If a guestbook object is found, call the view () method and use the Details View template to display the retrieved guestbook object. If no guestbook object is found, an error message is displayed using the notfound view template, indicating that the guestbook object does not exist (an overloaded view () you only need to input the view Template Name-view ("notfound ")).

3. Implement the notfound view template:

We started to implement the notfound view template-display a friendly error message, indicating that the requested message (guestbook) was not found.
First, place the cursor in a controller Action Method body, right-click, and select Add View menu item in the pop-up menu,
You can create a new view template, as shown in.


In the add view dialog box that appears, enter notfound, as shown in. By default, the view name in the dialog box matches the cursor
The view name in the Action method is the same. Here, the view name is details. Because the notfound view template is implemented in the first step
The name must be changed to notfound.


Click Add. Visual Studio creates a new notfound. aspx view template in the/views/dinners directory.
If the specified directory does not exist, the system automatically creates a directory.
By default, a view template has two content regions that can be used to place content and code. The first part allows us to customize html
The page title. The second part allows us to customize the main content of the HTML page ).

To implement the notfound entity template, we have added some basic content, as shown in:
<Asp: Content ID = "content1" contentplaceholderid = "titlecontent" runat = "server">
Message not found
</ASP: content>
<Asp: Content ID = "content2" contentplaceholderid = "maincontent" runat = "server">
<H2> message not found </H2>
<P> sorry-the message does not exist or has been deleted. </P>
</ASP: content>
Run the nerddinner sample program and enter/guestbook/details/123456 in the address bar of the browser. The guestbook does not exist in the Database. Therefore, the guestbookcontroller. Details () Action Method displays the notfound view template, as shown in:



You may notice that the view template we created inherits other HTML scripts and is displayed around the main content. This is because
The created view template uses the master page template to achieve consistent layout across the entire site.

4. Implement the Details View template:

Below, we will implement the Details View template to display the data of a single guestbook model. First, move the cursor to the details action method (in the guestbookcontroller code window of course), right-click, and select the Add View menu item in the pop-up menu.


Like creating a notfound view template in the previous step, the add view dialog box is displayed. Here, we use the default details as the view name. In addition, select the create a stronugly-typed view check box, select the model type that is passed from the Controller to the view from the drop-down list box. For the details view being created, we will pass the guestbook object (the complete name of this type is mvcapplication7.models. guestbooks ).
Note: For example, if you use the LINQ to SQL, drag the table to the O/R designer. After saving the table, you must regenerate the project to see it in creating a strong type.
As shown in:

Unlike the notfound view template for creating an empty view, here we select the details template. As shown in, select the details option in the View content drop-down list box. The system automatically generates an original implementation for the Details View template based on the imported guestbook model. In this way, we can quickly start the implementation of the view template.
After you click the Add button, Visual Studio automatically creates a details. aspx view template in the/views/guestbook directory.


In the details. aspx view template, an initial implementation has been created based on the guestbook model. The vs engine uses the. NET launch mechanism to query all the public attributes of the model class, and automatically adds the corresponding content to the view template based on the type of each field.


Next, run the web application and enter/guestbook/details/1 in the address bar to view the display information of the Details View. With this URL, We can display the records manually inserted in the guestbooks data table, as shown in:



This is simple and fast. The details. ASPX page provides an initial implementation. Next, we will further customize the user interface as needed. When we carefully check the details. ASPX page, we find that the page contains some static html and embedded display code. When the view template is rendered, the <%> code block is executed. <% = %> the code block is executed and the result is displayed in the view output.
You can also write code in the view to access the guestbook model object transmitted to the Controller by using the strong model attribute. When accessing the model attribute, the Visual Studio Editor provides code smart prompts, as shown in.


5. Third Party:

The auxiliary method is used to help generate an HTML interface in the view template.
The MVC Framework provides the following auxiliary methods:
(1) form auxiliary method-used for form elements, such as single-choice buttons, lists, drop-down lists, text boxes, textarea, hidden fields, and password fields.
(2) URL auxiliary method-let you generate the URL path.
(3) HTML auxiliary methods-provides methods for managing HTML strings, such as encode, decode, attributeencode, and renderpartial.

For example, there are two ways to generate a hyperlink. The first method is to manually compile the <A> element of HTML, and the <%> code block is embedded in the <A> element of HTML, as shown below:


The second method is to use ASP. net MVC built-in HTML. actionlink () is an auxiliary method. This method allows you to create HTML <A> elements programmatically and link another action method of the controller.
<% = Html. actionlink ("edit", "edit", new {id = model. ID}) %>

HTML. the first parameter of the actionlink () auxiliary method is the link text (the sample code is edited), and the second parameter is the Controller's Action Method Name (the sample code is the Edit Method ), the third parameter is a set of parameters passed into the action method (implemented in the form of anonymous name/value pairs ). In the above Code, specify the ID parameter of the linked guestbook. Because the default URL routing rule of ASP. net mvc is {controller}/{action}/{ID}, the following output is generated by the HTML. actionlink () auxiliary method:

<A href = "/guestbook/edit/1"> edit </a>

In the next section, we will introduce the local view.

Related Article

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.