Paul Hinett
Paul Hinett

Reputation: 1981

asp.net mvc - setting meta tags & page title

I have my asp.net mvc project setup which passes strongly typed view models to every view using a BaseViewModel. The base view model includes information such as page title & meta tag information.

Is there a fast & simple way to use this information from my ViewModel to set this information in my master page?

At the moment i have to include code such as this below in every view:

<asp:Content ID="Content1" ContentPlaceHolderID="HeadPlaceHolder" runat="server">
<meta name="keywords" content="<%= Model.MetaKeywords %>" />
<meta name="description" content="<%= Model.MetaDescription %>" /></asp:Content>

I can't think of a way to set this info automatically other than the way i am doing it currently, but just looking to optimise this repeated html code.

Thanks! Paul

Upvotes: 2

Views: 1507

Answers (2)

Colin Bowern
Colin Bowern

Reputation: 2192

Check out the ActionFilter in CodeCampServer as an example of another take on how to insert the title.

Upvotes: 0

Andy
Andy

Reputation: 2762

You might want to make the above code part of your master view and pass the Title, Keywords, and Description using ViewData dictionary from Controller to View.

Upvotes: 1

Related Questions