One of recurring questions in MVC is how to share data between views and master. The question must be reformulated : how to share data between ACTION and master.
The short answer is : Model of the View returned from Action have to put some data to the Model of the Master
The long answer here in 4 steps
Step 1: ensuring error.aspx page works fine
a)copy \Views\Shared\Site.Master into siteerror.master( the error.aspx inherits from a specialized model)
b) change in \Views\Shared\Error.aspx
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<System.Web.Mvc.HandleErrorInfo>" %>
to
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/SiteError.Master" Inherits="System.Web.Mvc.ViewPage<System.Web.Mvc.HandleErrorInfo>" %>
Step 2 : make the master strongly typed :
add a ModelMaster class
public class ModelMaster
{
public ModelMaster()
{
DataFromAction = "default data";
}
public string DataFromAction { get; set; }
}
and display this data to the html of \Views\Shared\Site.Master
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<MasterDataFromAction.Models.ModelMaster>" %> //code <h1>This is data shared from View : <%= Model.DataFromAction%></h1>
Step 3. Make the action return a strongly typed view. We will make , for example, the Index action from Home controller.
Add the ViewModelIndex class
public class ViewModelIndex :ModelMaster
{
public ViewModelIndex()
: base()
{
base.DataFromAction = "data from index";
}
}
and modify controller action and view
First Index action in \Controllers\HomeController.cs
public ActionResult Index()
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
ViewModelIndex vmi = new ViewModelIndex();
vmi.DataFromAction = "here comes data from index action";
return View(vmi);
}
Then the view :
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MasterDataFromAction.Models.ViewModelIndex>" %>
The result is here :
When do you do this things? The sooner, the better
Please find attached the project




#1 by redsquare at July 19th, 2010
| Quote
“When do you do this things?”
Never do this, use RenderAction instead for Master Page concerns
#2 by admin at July 19th, 2010
| Quote
could you make an example, please ?
#3 by in at July 22nd, 2010
| Quote
You could have set the ViewData in Index action of HomeController and display it into the Master as another option
#4 by admin at July 25th, 2010
| Quote
I do not like ViewData . It is not strongly typed.
#5 by eccsolutions at October 22nd, 2010
| Quote
I agree with Andrei. The best approach is one that is strongly typed. Plus, RenderAction runs into quite a few issues within the MasterPage. I prefer to use RenderPartial with strongly typed Models.
#6 by Andrei Ignat at October 27th, 2010
| Quote
The most simple way to deal with another models is :
<%=(Model!=null) ? Model.DataFromAction : “”%>
#7 by hugglebuddie at March 11th, 2011
| Quote
Excellent work! I agree with Andrei. The best approach is one that is strongly typed. Thanks!