Abandon the new solution of the page title in titlecontent-Asp.net MVC

Source: Internet
Author: User

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.

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.