On the template page of the official version of Asp.net MVC 1.0, the following information is provided:
<Asp: contentplaceholder id = "titlecontent" runat = "server"/>
This allows you to set the title of a page in each view independently. However, the disadvantage is that my page title is in the following format:
Uniform title-subtitle
Write in each view:
Diary page:
<Asp: Content ID = "content1" contentplaceholderid = "titlecontent" runat = "server">
Caraq-Diary
</ASP: content>
Album page:
<Asp: Content ID = "content1" contentplaceholderid = "titlecontent" runat = "server">
Caraq-album
</ASP: content>
......
Assume that one day I wantUniform titleIt is too troublesome to modify a page or page, and I think this naming method also carries the MVC spirit-let the Controller decide the content displayed in the view
In fact, there are simpler implementation methods as follows:
First, define the parent class of each controller as follows:
{
This. style. display = 'none'; document. getelementbyid ('Code _ closed_text_092039 '). style. display = 'none'; document. getelementbyid ('Code _ open_image_092039 '). style. display = 'inline'; document. getelementbyid ('Code _ open_text_092039 '). style. display = 'inline ';
} "Id =" code_closed_image_092039 "style =" display: none "> {
This. style. display = 'none'; document. getelementbyid ('Code _ open_text_092039 '). style. display = 'none'; getelementbyid ('Code _ closed_image_092039 '). style. display = 'inline'; getelementbyid ('Code _ closed_text_092039 '). style. display = 'inline ';
} "Id =" code_open_image_092039 ">
Code
Public Class Basecontroller: Controller
{
Private Readonly String _ Titleformat = " Caraq-{0} " ;
Private String _ Title;
Protected StringTitle
{
Get{Return_ Title ;}
Set{_ Title=Value ;}
}
Protected Override Void Onactionexecuted (actionexecutedcontext filtercontext)
{
Viewdata [ " Title " ] = String . Format (_ titleformat, title );
Base . Onactionexecuted (filtercontext );
}
}
To let all controllers inherit this parent class, you only need to use the following method in action to set the page title:
Public Class Blogcontroller: basecontroller
{
Public Actionresult index ()
{
This . Title = " Diary " ;
}
}
Finally, on the template page, replace the titlecontent placeholder control:
<% = Viewdata [ " Title " ] %>
In this way, we can see whether it is much easier to name the page title, and there will be no such image in the view.
<Asp: Content ID = "titlecontent" contentplaceholderid = "titlecontent" runat = "server"> </ASP: content>
To modifyUniform titleYou only need to modify the _ titleformat string in basecontroller.