ASP.NET MVC pass data from a view to master
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
“When do you do this things?”
Never do this, use RenderAction instead for Master Page concerns
could you make an example, please ?
You could have set the ViewData in Index action of HomeController and display it into the Master as another option
I do not like ViewData . It is not strongly typed.
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.
The most simple way to deal with another models is :
<%=(Model!=null) ? Model.DataFromAction : “”%>
Excellent work! I agree with Andrei. The best approach is one that is strongly typed. Thanks!
Hello Thanks,
But i Have question..!!
We have number of Action in controller.
So,
Is it that, I need to create MasterModel object in each and every Actions.Isn’t It ? Is there any other way ?
Yes, it’s best practice.
Hello would you mind letting me know which hosting company you’re working with? I’ve loaded
your blog in 3 completely different web browsers and
I must say this blog loads a lot quicker then most.
Can you suggest a good hosting provider at a fair price?
Cheers, I appreciate it!
livehosting