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


Posted

in

by

Comments

12 responses to “ASP.NET MVC pass data from a view to master”

  1. […] This post was mentioned on Twitter by ignatandrei and Hire ASP.Net Experts, Larry King. Larry King said: ASP.NET MVC pass data from a view to master « A Programmer with … http://bit.ly/cn5AbC #MVC […]

  2. redsquare Avatar
    redsquare

    “When do you do this things?”

    Never do this, use RenderAction instead for Master Page concerns

    1. admin Avatar
      admin

      could you make an example, please ?

  3. in Avatar
    in

    You could have set the ViewData in Index action of HomeController and display it into the Master as another option

    1. admin Avatar
      admin

      I do not like ViewData . It is not strongly typed.

  4. eccsolutions Avatar
    eccsolutions

    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.

  5. Andrei Ignat Avatar
    Andrei Ignat

    The most simple way to deal with another models is :
    <%=(Model!=null) ? Model.DataFromAction : “”%>

  6. hugglebuddie Avatar

    Excellent work! I agree with Andrei. The best approach is one that is strongly typed. Thanks!

  7. HImen Avatar

    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 ?

    1. Andrei Ignat Avatar
      Andrei Ignat

      Yes, it’s best practice.

  8. epicscape668.smffy.com Avatar

    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!

    1. Andrei Ignat Avatar
      Andrei Ignat

      livehosting

Leave a Reply

Your email address will not be published. Required fields are marked *