fusioninternet
asked on
MVC3 assign variable on every page from single peice of code
Hi experts.
I'm new to MVC3 coming from an asp.net background.
I have a multi page size using razor views and a master layout page.
On the master page is a div that contains a banner image and what i wish to do is change that image depending on what page i go to.
So, what trying to find out is how to run code from the master page to populate a variable on the master page? In asp.net i would normally put query in the master page's .cs file and either push the variable to the front page or pull the variable from the code.
I realise that i could create a public void and call this from the each pages action result, but I'm sure there must be a way to put the code in once and have it called automatically by each page.
Regards
Steve
I'm new to MVC3 coming from an asp.net background.
I have a multi page size using razor views and a master layout page.
On the master page is a div that contains a banner image and what i wish to do is change that image depending on what page i go to.
So, what trying to find out is how to run code from the master page to populate a variable on the master page? In asp.net i would normally put query in the master page's .cs file and either push the variable to the front page or pull the variable from the code.
I realise that i could create a public void and call this from the each pages action result, but I'm sure there must be a way to put the code in once and have it called automatically by each page.
Regards
Steve
http://forums.asp.net/t/1207291.aspx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Option:1
<img src="@ViewData["masterImag e"]" title="src="@ViewData["mas terImage"] "" />
Manipulate ViewData in the basecontroller suggested by sammy1971
Option:2
Create HTML helper class
public static MvcHtmlString MasterImage(this HtmlHelper html)
{
/*
Action and Controller name can be found in the html object.
Based on different action return image path and use it below.
Switch(action)
{
case "action1": imagepath = "\image1.jpg"; break
case "action2": imagepath = "\image2.gif"; break
}
*/
var imgBuilder = new TagBuilder("img");
imgBuilder.MergeAttribute( "src", url.Content(imagePath));
imgBuilder.MergeAttribute( "alt", alt);
string imgHtml = imgBuilder.ToString(TagRen derMode.Se lfClosing) ;
return MvcHtmlString.Create(imgHt ml );
}
use in master page
@Html.MasterImage()
<img src="@ViewData["masterImag
Manipulate ViewData in the basecontroller suggested by sammy1971
Option:2
Create HTML helper class
public static MvcHtmlString MasterImage(this HtmlHelper html)
{
/*
Action and Controller name can be found in the html object.
Based on different action return image path and use it below.
Switch(action)
{
case "action1": imagepath = "\image1.jpg"; break
case "action2": imagepath = "\image2.gif"; break
}
*/
var imgBuilder = new TagBuilder("img");
imgBuilder.MergeAttribute(
imgBuilder.MergeAttribute(
string imgHtml = imgBuilder.ToString(TagRen
return MvcHtmlString.Create(imgHt
}
use in master page
@Html.MasterImage()
ASKER
Yes basecontroller was the way to go and got it to work as i wnated once I managed to find out where to create a base controller and how to reference it