ASP. net mvc Razor view (2), mvcrazor

Source: Internet
Author: User

ASP. net mvc Razor view (2), mvcrazor
Yesterday, I introduced some basic Razor syntaxes. I went on to add a new Razor into a few days. The last time I introduced how to output the original text, I used the <text> </text> label, next we will introduce another Syntax: @ {@: I love Beijing} This @: I love Beijing must be written in the code segment @ {} C #; otherwise, an error is reported.

 

1. How do I output the @ symbol?

In Razor, we use @ to output "@", which is similar to "//" in C "//"

2. We can use Response. Write (), Server, Request, Session in Razor. Why?

We decompile. cshtml to generate a foreground class that inherits WebViewPage <object>

We didn't see the Session, Server, and other attributes in the foreground class, so we went to find its parent class WebViewPage <object>, and the parent class was not found, and we found the parent class of the parent class, and so on

 

The WebPageRenderingBase class is found, which defines the desired attribute. 3. How do I define methods in the Razor view? Here we use:
@ Helper Hello (string a) {<span> I Love Tiananmen Square in Beijing @ a </span >}@ Hello ("the sun rises on Tiananmen Square ")
As mentioned above, the view will be compiled into a foreground class. This class has an Execute () method that concatenates static content on the view and C # code for String concatenation, if I define a method on a view, wouldn't I define a method in the method? This is a question, so we solve these questions through decompilation.

We can see the Hello method defined in the view, and the return value is HelperResult. Here we can know that as long as the return value of the method defined in the view is HelperResult class. so there is no way to define methods in the method. Now I have another question. The reason we adopt the Mvc model is to separate the UI from the business. Do you define the business in the UI multiple times? Explanation: we generally do not define methods on the view. The purpose of defining methods is to solve the problem where Html code must be repeatedly output on The View, for example, I want to output <span> I Love Tiananmen Square in Beijing </span> multiple times in different positions on the view, because the for loop cannot be used for different positions. Writing multiple times is a burden on programmers, the method we defined is used here.

 

4. request another view in one view. Assume that the Index method under the Home controller has an Index. in the cshtml view, I want to request a Check from the Home controller in the Index view. cshtml view. So this is used.
// Use the code snippet {}@{ Html. renderPartial ("check");} // call the view @ {Html. renderPartial ("~ /Views/Dept/Index. cshtml ");} // If You Want To transmit data from the current view to the check view, you can use ViewBag, ViewData, TempData, etc. @ {ViewBag. CName = "hello"; Html. renderPartial ("check ");}
You do not need to add @ {} to call a method with a return value in the view. It can be seen that @ is directly followed by a variable or a method with a return value, and the content of the variable or the return value of the method is directly output here.
@Html.TextBox("txtname")

 

Another method is @ {Html. RenderAction ("check");}.

From the above two figures, we can see that they can request another view from one view, but the two request methods are not the same. Using the RenderPartial method, I directly request check in the Index view. cshtml, and RenderAction is to request/home/check. The two methods are different. However, when you view the source code of the webpage, you will find that the requested view contains

This is not very friendly. Next time, I will introduce the distribution view. I hope you can correct your official criticism.

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.