In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to use the ASP.NET MVC3 template page". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Create a new MVC3 project, and in the solution resource management, we can see a Shared folder under the VIEWS folder. There is a _ Layout.cshtml page in the Shared folder. This is the default template page in the project. As shown in the following figure:
1. Create a new content page
The content page is also called the view layout page in MVC3. You can right-click the view folder, add the content page by adding the view, and then select the appropriate template. Here is the content page we created, ViewPageOne.cshtml
A page that uses a template page automatically generates code in the page. To specify a template page, use Layout to specify a specific template page. If there is no Layout property in the content page, the default template page is used
@ {ViewBag.Title = "ViewPageOne"; Layout = "~ / Views/Shared/_Layout.cshtml";} ViewPageOne
If Layout is specified as null, no template page is used
@ {ViewBag.Title = "ViewPageOne"; Layout = null;}
two。 Create a new template page
Right-click the Shared file, then select add New item from the menu, pop up the add New item dialog box, and then select the MVC3 layout page, so the layout page is added. The layout page _ LayoutPageOne.cshtml that we added below
3. Specify a template page separately for a file
If a page does not want to use the default template page, you can specify a template page for it separately. The following code specifies the template page of the content page as the template page _ LayoutPageOne.cshtml that you just created
@ {ViewBag.Title = "ViewPageOne"; Layout= "~ / Views/Shared/_LayoutPageOne.cshtml";} ViewPageOne
4. Specify a template page for a view folder
If you want all views under a Controller to use the same template file, you can create a _ ViewStart.cshtml file under the attempt folder corresponding to the Controller, and then specify the template page to be used in _ ViewStart.cshtml
@ {Layout = "~ / Views/Shared/_LayoutPageOne.cshtml";}
This allows you to specify a view under controller to use a template file
5. Pages do not use template pages
If a content page does not want to use template pages, you can set _ Layout to null
You can also add a view page without selecting a template.
6.@RenderBody ()
@ RenderBody () is used in the template page to indicate the location of the content page in the template. When you create a content page with a template page, the content page is rendered in the same location as @ RenderBody () in the template page, and there can be only one @ RenderBody () in a template page.
7.@RenderSection
@ RenderSection is used to define an area in the template layout, which can be populated by defining some content on the content page, such as the JS file referenced in the content page, which can be populated to the section location of the template page. Some separate information for each content page can be displayed in this area of the template page.
@ RenderSection has two parameters. The first parameter is used to define the name of section, and the second parameter is of Boolean type. If TRUE, the content page must define the section. If it is false, it means that the content page can define section or not.
Template page:
Welcome @ ViewBag.Title
Second template page
@ RenderBody () remarks: @ RenderSection ("Remark", false)
Content page:
@ {ViewBag.Title = "home page";} @ ViewBag.Message
To learn more about ASP.NET MVC, visit http://asp.net/mvc.
@ section Remark {I am the home page}
Welcome
8.@RenderPage
RenderPage is used to represent the content of one page rendered in another page. Parameter specifies the location of the web page to render.
We create a new footer.cshtml file under the Shared folder, then write the copyright information in it, and then open a place in the template page to render the footer page.
Welcome @ ViewBag.Title
Second template page
@ RenderBody () remarks: @ RenderSection ("Remark", false); @ RenderPage ("~ / Views/Shared/footer.cshtml")
9.@Html.Partial () and Html.RenderPartial ()
. @ Html.Partial () and Html.RenderPartial () can be used to output a partial page, right-click to add a new item, select MVC3 partial page, you can add a partial page, which is equivalent to a user control
At this point, you can use two methods to output the contents of the partial page, of course, these two methods can also directly output the view page and the content page.
The argument to @ Html.Partial () is the name of the user control, which is the name of the partial page, and the return value is of type string, which can be output directly.
The parameter of @ Html.RenderPartial () is also the name of the user control, the return value is VOID, and the content is output directly to Response when called.
The usage is as follows:
@ Html.Partial ("_ PartialLogin") @ {Html.RenderPartial ("_ PartialLogin");} "how to use the ASP.NET MVC3 template Page" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.