ASP. Net MVC layout page template page usage detailed introduction

  • 2021-10-11 18:06:20
  • OfStack

1. Views folder- > _ Layout. cshtml Master page under Shared folder

@RenderBody

When you create a view based on the _ Layout. cshtml layout page, the contents of the view are merged with the layout page, and the contents of the newly created view are rendered between the tags through the @ RenderBody () method of the _ Layout. cshtml layout page.

@RenderPage
It can be guessed from the name that this method is to render 1 page. For example, a fixed header in a web page can be placed separately in a shared view file, and then called by this method in the layout page. The usage is as follows:
@ RenderPage ("~ /Views/Shared/_Header. cshtml")
With parameters
@ RenderPage ("~ /Views/Shared/_Header. cshtml", new {parm= "my", parm2= "you")
Call the page to get parameters:
//Get the parameters passed by RenderPage ()
@PageData["param"]

@RenderSection

The layout page also has the concept of a section (Section), which means that if a section is defined in a view template, it can be rendered separately
To prevent exceptions due to missing sections, you can give RenderSection () a second parameter:
@RenderSection("head", false)
Or
@if (IsSectionDefined("head"))
{
@RenderSection("head", false)
}
else
{
< p > SubMenu Section is not defined! < /p >
}

The code is as follows:


<!DOCTYPE html> 
<html> 
<head> 
  <title>@ViewBag.Title</title> 
  <link href="@Url.Content(" rel="external nofollow" ~/Content/Site.css")" rel="stylesheet" type="text/css" /> 
  <script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script> 
  @RenderSection("head", required: true)@*View Page customization specific js/css Use *@ 
</head> 
 
<body> 
  @RenderPage("~/Views/Shared/_Header.cshtml") 
  @RenderBody() 
</body> 
</html> 

2. Create a view, using a master page

The code is as follows:


@{ 
  ViewBag.Title = "Index"; 
  Layout = "~/Views/Shared/_Layout.cshtml"; 
} 
<h2>Index</h2> 
@section Head{ 
  <script type="text/javascript"> 
    $(function () { 
      alert("hello jquery"); 
    }); 
  </script> 
} 
<p> Execute C# General grammar </p><br /> 
@DateTime.Now.Date.ToShortDateString() 
 
<p> Execute C# Statement segment </p> 
@{ 
  List<string> list = new List<string> { "Mvc3", "Razor" }; 
  list.Add(".Net4");   
} 
<ul> 
@foreach(string s in list) 
{ 
  if (string.IsNullOrEmpty(s)) 
  { 
    <li> Empty </li> 
  } 
  else 
  {  
    <li>@s</li> 
  } 
} 
</ul> 

3. Generate the source code for the page


<!DOCTYPE html>
<html>
<head>
  <title>Index</title>
  <link href="/Content/Site.css" rel="external nofollow" 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>

</head>

<body>
  <h2>Index</h2>

<p> Execute C# General grammar </p><br />
2013/3/11

<p> Execute C# Statement segment </p>
<ul>
    <li>Mvc3</li>
    <li>Razor</li>
    <li>.Net4</li>
</ul>

 

</body>
</html>

4. @Html.Partial

Partial creates its own instance of TextWriter each time and caches the contents in memory. Finally, all writer outputs are sent to an MvcString object
More often than not, we use @ {Html.RenderPartial ("Details");} instead of @ Html.Partial

The difference between Html. RenderPartial () and @ Html.Partial

Html. RenderPartial outputs directly to the current HttpContext (good performance because it is a direct output).

Html. Partial generates a string directly from the view contents and returns it (equivalent to an escape process).

Differences between RenderPage () and RenderPartial ()

The page called by RenderPage () can only use it to pass past data.
RenderPartial () can use viewdata, model and other data.

For example: @ {Html. RenderPartial ("BasicChart", model);}
With this overload, you can use strong typing in some views, and then pass model in the main view with the second parameter

@{Html.RenderPartial("BasicChart",ViewData["myData"]);}

Html. RenderPartial and Html. RenderAction

Html. RenderPartial is suitable for reuse UserControl and only needs to be presented through Model, or UserControl for advertising is also suitable.


Html. RenderAction will call Action method of Controller first. If this UserControl needs to obtain data through database to present (read database through Action), it will be more suitable to use this method.

Additional:

1. The return value of the method with Render is void, which is output inside the method; The return value type without is MvcHtmlString, so it can only be used as follows:
@ 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, while Html. Action needs a corresponding Action, and returns PartailResult inside Action (that is, retun PartialView ()).

3. For simple user controls without any logic, it is recommended to use Html. Partial; Html. Action is recommended for user controls that require a few Model settings. Of course, the Html. Partial method can also be used with Model data, and you can see the overload of the method.

4. One advantage of using Html. Action is that you can choose different user controls according to different scenarios.

For example:
@Html.Action("UserInfoControl")
In the corresponding UserInfoControl, Action, when the user is not logged in, retun PartialView ("LogOnUserControl") can be used; After logging in, you can use retun PartialView ("UserInfoControl");


Related articles: