views:

405

answers:

2

Hi,

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

A: 

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

Colin Bowern
The link above is broken.
Simon Lomax
They moved to CodePlex, updated the link.
Colin Bowern
+1  A: 

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.

Andy