ASP. net mvc view (4), asp. netmvc View

Source: Internet
Author: User

ASP. net mvc view (4), asp. netmvc View
ASP. NET MVCView(Thu)Preface

In the previous article, I used the IoC framework to inject dependencies into the implementation of views. Finally, I would like to briefly introduce how custom view guides are defined and used, the details of the Razor syntax and the use of the auxiliary tool are described in the next article. This article describes the concept of segmentation in the view and the use of the segment view.

 

ASP. NET MVC View
  • Custom view Engine
  • RazorView engine Execution Process
  • RazorView dependency injection and custom view guides
  • Use of segment and segment views
  • RazorSyntax and view guides

 

Use of segment and segment views

 

Use of segments

The Razor engine in the ASP. net mvc framework has the concept of segmentation. What does segmentation mean? A segment refers to dividing a view into several custom segments and handing it over to the layout to control the display of segments. In this case, let's take a look at the Definition of the code. First, we use the definition of the segmented view code. Example code 1-1.

Code 1-1

@ {ViewBag. title = "ViewPartCase" ;}< h2> ViewPartCase 

In code 1-1, use @ section between labels to create segments, followed by the names of custom segments. In this case, the layout code is shown in the sample code 1-2.

Code 1-2

<!DOCTYPE html>

Result 1 at this time.

Figure 1

The error message shown in Figure 1 is clearly displayed. In the layout, PartOne, PartTow, and PartThree are not defined.

In this way, we can only modify code 1-2, modify the definition in the layout, and the modified sample code 1-3.

Code 1-3

<!DOCTYPE html>

In code 1-3, the @ RenderSection label is used to display the specified segment. The parameter is the custom segment name. here we need to understand the concept that the layout definition is used to guide the definition of the segment of the view, however, in this article, we have reversed the order to give everyone a clear understanding. In actual use, we should first define the segments used in the layout and the names of the segments indicated, in the view definition, if you want to use the current layout, the content is defined based on the specified segment name in the current layout. Check the above Code Execution result 2.

Figure 2

For example, now I want to define code 1-3 first, and then I want to define the page that I want to render, that is, our view. The definition like code 1-1 is the normal application order, now I want to modify code 1-1 because I don't want to use segments in my view. Example code 1-4

Code 1-4

@{    ViewBag.Title = "ViewPartCase";}

As you can see, code 1-4, that is, the modified view, displays a title and does not want anything else. Run result 2 again.

Figure 3

Figure 3 clearly indicates that the PartOne segment is not defined in the view, so this error will occur. Even if PartOne is defined and PartThree is defined, an error will be reported, this is mandatory, right.

There are two ways to deal with this situation,

First, use the IsSectionDefinde () method. The method parameter is the segment name to determine whether the segment with this name is defined in the view. If yes, true is returned, otherwise, false is returned (the following code provides an example ).

Second, use the overload version of the @ RenderSection () method to set the second parameter to false. If the view contains the segment specified by the method parameter, no exception is reported.

Now let's take a look at the code 1-3 in the layout code to demonstrate the two situations mentioned above. Example code 1-5.

Code 1-5

<!DOCTYPE html>

Now let's take a look at figure 4 ,,

Figure 4

 

Use of the branch view

The use of the branch view is actually the same as that of the view, but the difference between the distribution view and the view is that the branch view disables the layout, that is to say, the use of segments in the segment view mentioned in the above section is meaningless. Let's take a look at the definition of the segment view.

Figure 5

Figure 5 does not create a strong division view. This is of little significance. After you click the "add" button, the displayed code page is blank and there is no page, here we will show you how the branch view shows the type after compilation, that is, it is no different from the common view. Example code 1-6.

Code 1-6

public class _Page_Views_iocdemo_CustomPartialView_cshtml : System.Web.Mvc.WebViewPage<dynamic> {        #line hidden                public _Page_Views_iocdemo_CustomPartialView_cshtml() {        }                protected ASP.global_asax ApplicationInstance {            get {                return ((ASP.global_asax)(Context.ApplicationInstance));            }        }                public override void Execute() {WriteLiteral("

Code 1-6 is displayed as in ASP. as shown in the final example in net mvc view (2), the core is not changed. Let's delete this branch view and add a strongly-typed view with the same name, or directly use the @ model label to reference A type as the Model type of the view. Example code 1-7.

Code 1-7

@ Model MvcApplication. models. customerInfo 

Last rendered interface 6.

Figure 6

 

 

Author: Jin Yuan

Source: http://www.cnblogs.com/jin-yuan/

The copyright of this article is shared by the author and the blog Park. You are welcome to reprint this article. However, you must keep this statement without the author's consent and go to the Article Page.


Some view rendering problems with ASPNET MVC

No. if Javascript is used in a local view, you can reference js When referencing a local view in other views. That is to say, when a view references a local view, all resources of the local view are referenced. in your case, js is not loaded.

Differences between several controllers in aspnet MVC40

Www.cnblogs.com/..w.html
 

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.