Yesterday introduced some Razor basic grammar, a few days went down to say: Fill in a, last introduced how to output the original text, with <text></text> tags, the following again introduced a syntax: @{@: I love Beijing} This @: I love Beijing must be written in @{}c #代码段中, otherwise the error
1. How do I output the @ symbol?
In razor, we output "@" with @@ 输出 similar to "//" in C #
2. We can use Response.Write () in Razor, server,request,session, what is this for?
We decompile. cshtml, which generates a foreground class that inherits the Webviewpage<object>
We do not see the session in the foreground class, the server and so on, then to find its parent class Webviewpage<object>, and the parent class did not find, and then find the parent class of the parent class, and so on, and finally in
Found in the Webpagerenderingbase class, it defines the attributes we want in the inside. 3. How do I define a method in the Razor view? Here we use:
@helper Hello (string a) { <span> I love Beijing Cheonan @a</span> } @Hello ( " The sun rises on Tiananmen Square ")
As I mentioned earlier, the view is compiled into a foreground class that has an execute () method that will stitch the static content on the view and the C # code, and if I define the method on the view, will it be defined in the method? This is a question, so we can solve these problems by anti-compilation.
We can see from the above that we define the Hello method in the view, the return value is Helperresult, here we can know as long as you define on the view of the method return value is the Helperresult class. So there's no way to define a method in a method. Now there's another question, the reason we're using the MVC pattern is that it separates the UI from the business, and you define the business in the UI. This is not a lot of times. Explanation: We don't typically define methods on a view, and the purpose of defining the method is to resolve the situation where the HTML code is repeated on the view. For example, I'm going to have multiple outputs in different positions on the view. <span> I love Beijing Tian ' an gate </span>, because the location can not be used for a loop, multiple write to the programmer has a burden, here is the method we define.
4. Request another view on one view assuming that our index method under the home controller has a index.cshtml view, now I want to request a check.cshtml view of the home controller on the index view. Well, that's the use.
//a method that calls no return value in a view must use the code snippet @{}@{html.renderpartial ("Check");}//call a view under another view file@{html.renderpartial ("~/views/dept/index.cshtml");}//to pass data from the current view to the check viewYou can use ViewBag, ViewData, tempdata, etc. @{viewbag.cname="Oh, hello."; Html.renderpartial ("Check");}
Calling a method in a view that has a return value does not require a @{}, so you can see that the value is directly followed by a variable or a method with a return value, where the variable contents or method return values are directly output
@Html. TextBox ("txtname")
Another way to do that is @{html.renderaction ("Check");}
It can be seen from the above two graphs that they can request another view from one view, but two requests are not the same, using the RenderPartial method I request check.cshtml directly on the index view, and Renderaction is going to request/home/ Check, two different ways. But when you look at the source code of the Web page, you will see that the requested view contains
This is not very friendly. Next time I'll cover the distribution view. I hope you crossing criticize me.
ASP. NET MVC Razor View (2)