ASP. NET MVC Layout page template page How to use details

Source: Internet
Author: User

I. _layout.cshtml master page under the shared folder, views folder

@RenderBody

When you create a view that is based on a _layout.cshtml layout page, the contents of the view are merged with the layout page, and the content of the newly created view is passed through the @renderbody () of the _layout.cshtml layout page Methods are rendered between labels.

@RenderPage
From the name can guess this method is to render a page. For example, the fixed head of the Web page can be placed in a shared view file, and then in the layout page through this method call, use the following:
@RenderPage ("~/views/shared/_header.cshtml")
With parameters
@RenderPage ("~/views/shared/_header.cshtml", new{parm= "my", parm2= "You")
Invoke page Get Parameters:
Gets the arguments passed by the Renderpage ().
@PageData ["Param"]

@RenderSection

The layout page also has the concept of section, which means that if a section is defined in a view template, it can be rendered separately
To prevent exceptions due to a missing section, you can provide a 2nd parameter to Rendersection ():
@RenderSection ("Head", false)
Or
@if (issectiondefined ("Head"))
{
@RenderSection ("Head", false)
}
Else
{
<p>submenu section was not defined!</p>
}

The code is as follows:

  1. <! DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <title> @ViewBag title</title>
  5. <link href="@Url. Content (" ~/content/site.css ")" rel="stylesheet" type=" Text/css " />
  6. <script src="@Url. Content (" ~/scripts/jquery-1.4.4.min.js ")" type="Text/javascript" ></script>
  7. @RenderSection ("Head", required:true) @*view page Custom specific js/css use *@
  8. </head>
  9. <body>
  10. @RenderPage ("~/views/shared/_header.cshtml")
  11. @RenderBody ()
  12. </body>
  13. </html>


Second, create the view, use the master page

The code is as follows:

  1. @{
  2. viewbag.title = "Index";
  3. Layout = "~/views/shared/_layout.cshtml";
  4. }
  5. <h2>index</h2>
  6. @section head{
  7. <script type="Text/javascript">
  8. $ (function () {
  9. Alert ("Hello jquery");
  10. });
  11. </Script>
  12. }
  13. <p> Execute C # normal syntax </P><br />
  14. @DateTime. Now.Date.ToShortDateString ()
  15. <p> Execute C # statement segment</P>
  16. @{
  17. List<string> list = new list<string> {"Mvc3", "Razor"};
  18. List. Add (". Net4 ");
  19. }
  20. <ul>
  21. @foreach (string s in list)
  22. {
  23. if (string. IsNullOrEmpty (s))
  24. {
  25. <li> empty </li>
  26. }
  27. Else
  28. {
  29. <li>@s</li>
  30. }
  31. }
  32. </ul>


Third, generate the source code of the page

<! DOCTYPE html>
<title>Index</title>
<link href= "/content/site.css" rel= "stylesheet" type= "Text/css"/>
<script src= "/scripts/jquery-1.4.4.min.js" type= "Text/javascript" ></script>

<script type= "Text/javascript" >
$ (function () {
Alert ("Hello jquery");
});
</script>

<body>

<p> Execute C # common syntax </p><br/>
2013/3/11

<p> Execute C # statement segments </p>
<ul>
<li>Mvc3</li>
<li>Razor</li>
<li>. Net4</li>
</ul>

</body>

Iv. @Html. Partial

The Partial creates its own TextWriter instance each time and caches the content in memory. Finally, the contents of all writer outputs are sent to a Mvcstring object.
More often we will use @{html.renderpartial ("Details");} And not @html.partial.

The difference between html.renderpartial () and @html.partial

Html.renderpartial output directly to the current HttpContext (because it is a direct output, so the performance is good).

Html.partial generates a string directly from the view content and returns (equivalent to having an escaped procedure).

The difference between renderpage () and renderpartial ()

A page called by Renderpage () can only use it to pass past data.
RenderPartial () can use data such as Viewdata,model.

such as: @{html.renderpartial ("Basicchart", model);}
With this overload, you can use a strong type in a partial view, and then use the second parameter in the main master to pass the model past

@{html.renderpartial ("Basicchart", viewdata["MyData"]);}

The difference between html.renderpartial and html.renderaction

Html.renderpartial is suitable for use in repeated use of the UserControl, and only need to show the content through the model, or for advertising UserControl is also suitable.


Html.renderaction will call the Controller's action method first, if this UserControl need to get data through the database to render (through action to read the database), it is more appropriate to use this method.

Add:

1. The return value of the method with render is void, output inside the method, and the return value type is mvchtmlstring, so this can only be used:
@Html. Partial corresponds to @{html.renderpartial (...);}
@Html. Action corresponds to @{html.renderaction (...);}
2, html.partial can directly provide the user control name as a parameter, and html.action need to have a corresponding action, within the action return Partailresult (that is, Retun Partialview ()).
3, for a simple user control without any logic, it is recommended to use html.partial; for user controls that need to set some model, Html.action is recommended. Of course, there are model data can also use the Html.partial method, you can see the overloads of the method.
4, the use of html.action has the advantage that can be based on different scenarios to choose a different user control.
Like what:
@Html. Action ("Userinfocontrol")
In the corresponding Userinfocontrol this action, when the user is not logged in, you can Retun Partialview ("Logonusercontrol"), after logging in, you can Retun Partialview (" Userinfocontrol ");

Reprint, Source address: http://www.cnblogs.com/xlhblogs/archive/2013/06/09/3129449.html

ASP. NET MVC Layout page template page How to use details

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.