ASP. NET MVC View (iv)

Source: Internet
Author: User

ASP . NET MVC View ( four )

Objective

for the use of The IoC framework depends on the implementation of the view, and at the end of this article simply introduces how the Custom views helper is defined and used, the details of the Razor syntax, and the use of the helper. In this article, we explain the concept of segmentation in the view, And the use of partial views.

Asp.netmvc View

l Custom View engine

l Razor View Engine Execution procedure

l Razor Dependency Injection, Custom view helper for views

l segmentation, use of partial views

l Razor syntax, view helper

segmentation, use of partial views

Use of segments

in the The Razor engine in the ASP. NET Framework has a segmented concept, what does it mean? Fragmentation means dividing a view into a few custom segments and leaving the layout to control the display of the segments. To say so, let's take a look at the definition of the code, first using the definition of the segmented view Code, example code 1-1.

Code 1-1

@{viewbag.title = "Viewpartcase";} 


in code 1-1 , use the @section to create segments, followed by the name of the custom segment, which is the code for the layout of example code 1-2.

Code 1-2

<! DOCTYPE html>


this time the result of running 1.

Figure 1

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/40/EC/wKioL1PPnobxwtQjAALtT2HVzOU906.jpg "title=" View1.png "width=" 738 "height=" "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:738px;height:220px; "alt=" Wkiol1ppnobxwtqjaaltt2hvzou906.jpg "/>

obviously you see the error message that is indicated in Figure 1, Partone,Parttow,partthree are not defined in the layout .

so we can only modify the code 1-2, modify the layout of the definition, the modified example code 1-3.

Code 1-3

<! DOCTYPE html>


in code 1-3 @RenderSection label is used to display the specified segment, and the parameter is a custom segment name, so here's the idea that the definition of layout is to guide the definition of the view segment, Only in this space is to make it clear that the order is reversed, the actual application should be to define the layout of the segment to be used, as well as the indicated segment name, and then in the definition of the view if you want to use the current layout will be based on the current layout of the specified segment name to define the content. Look at the above code running result 2.

Figure 2

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/40/EC/wKioL1PPnquxRrDEAAEpxvUJR78602.jpg "title=" View2.png "alt=" Wkiol1ppnquxrrdeaaepxvujr78602.jpg "/>

For example, now I'm defining the code 1-3, and then I want to define the page that I want to render, which is our view, as defined by code 1-1 is the normal order of application, I now modify the code 1-1, Because I don't want to use fragmentation in my view, example code 1-4

Code 1-4

@{viewbag.title = "Viewpartcase";} 


as you can see, the code 1-4, which is my modified view, is to display a title, do not want to have other things, run the result 2 again .

Figure 3

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/40/EC/wKiom1PPna7A6_btAAIW0tFTkXc189.jpg "title=" View3.png "width=" 738 "height=" 373 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:738px;height:373px; "alt=" Wkiom1ppna7a6_btaaiw0tftkxc189.jpg "/>

The explicit indication in Figure 3 that a fragment named Partone is not defined in the view will cause such an error, even if partoneis defined, and partthree is defined , or then error, this is mandatory constraints, so much less humanitarian ah.

In the face of this situation, we have two ways of dealing with this situation,

first, the use of The Issectiondefinde () method, which is the fragment name, determines whether a fragment of this name is defined in the view and returns True if it is defined,otherwise false (thefollowing code gives an example).

second, using the overloaded version of the @RenderSection () method, the second argument is set to false, and if the view contains a segment specified by the method parameter, it will be displayed, and no exception will be reported.

now let 's take a look at the code in the layout code 1-3 to illustrate the two cases mentioned above, example code 1-5.

Code 1-5

<! DOCTYPE html>


this time we'll look at the results . Figure 4,

Figure 4

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/40/EC/wKiom1PPncqD3ebtAABCnrqI4Ho266.jpg "title=" View4.png "alt=" Wkiom1ppncqd3ebtaabcnrqi4ho266.jpg "/>

Use of partial views

The use of partial views is actually the same as the view, but the difference between the distribution view and the view is that the partial view is disabled layout, that is to say the above section in the partial view of the use is meaningless, let's look at the definition of the partial view

Figure 5

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/40/EC/wKioL1PPnu7B6nRiAAGMa-Vt5og481.jpg "title=" View5.png "alt=" Wkiol1ppnu7b6nriaagma-vt5og481.jpg "/>

Figure 5 does not create a strongly-typed partial view, this does not make much sense, after clicking the "Add" button, the rendered code page is an empty page nothing, The main point here is to show you what the partial view looks like after the compiled type is no different from the normal 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 ()  {        }                 protectedASP.global_asax  applicationinstance {            get{                 return ((ASP.global_ asax) (context.applicationinstance));             }         }                 Public override voidexecute ()  {writeliteral ("


The code 1-6 is rendered just like the last example in ASP. NET MVC view ( two ) , where the core is immutable, and we're still going to delete the partial view. Re-add an equally named strongly typed view, or use the @model tag directly to refer to a type as the model type of the view , example code 1-7.

Code 1-7

@model MvcApplication.Models.CustomerInfo 


The final rendering of the interface 6.

Figure 6

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/40/EC/wKioL1PPnw2DDgbAAAB3uMAlybk796.jpg "title=" View6.png "alt=" Wkiol1ppnw2ddgbaaab3umalybk796.jpg "/>


This article is from the "Jinyuan" blog, please be sure to keep this source http://jinyuan.blog.51cto.com/8854733/1528432

ASP. NET MVC View (iv)

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.