<p>What is your way of passing data to Master Page (using ASP.NET MVC) without breaking MVC rules?</p>

<p>Personally, I prefer to code abstract controller (base controller) or base class which is passed to all views.</p>

[{'answer_id': 78571, 'author': 'Ian P', 'author_id': 10853, 'author_profile': 'https://Stackoverflow.com/users/10853', 'pm_score': 3, 'selected': False, 'text': "<p>Abstract controllers are a good idea, and I haven't found a better way. I'm interested to see what other people have done, as well.</p>\n"}, {'answer_id': 78576, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>The Request.Params object is mutable.  It's pretty easy to add scalar values to it as part of the request processing cycle.  From the view's perspective, that information could have been provided in the QueryString or FORM POST.  hth</p>\n"}, {'answer_id': 78594, 'author': 'David Negron', 'author_id': 981, 'author_profile': 'https://Stackoverflow.com/users/981', 'pm_score': 2, 'selected': False, 'text': '<p>I did some research and came across these two sites. Maybe they could help.</p>\n<p><a href="https://stephenwalther.com/archive/2008/08/12/asp-net-mvc-tip-31-passing-data-to-master-pages-and-user-controls" rel="nofollow noreferrer">ASP.NET MVC Tip #31 – Passing Data to Master Pages and User Controls</a></p>\n<p><a href="https://web.archive.org/web/20200221095802/http://geekswithblogs.net:80/scarpenter/archive/2007/12/14/117724.aspx" rel="nofollow noreferrer">Passing Data to Master Pages with ASP.NET MVC</a></p>\n'}, {'answer_id': 78616, 'author': 'dimarzionist', 'author_id': 10778, 'author_profile': 'https://Stackoverflow.com/users/10778', 'pm_score': 0, 'selected': False, 'text': '<p>I thing that another good way could be to create Interface for view with some Property like ParentView of some interface, so you can use it both for controls which need a reference to the page(parent control) and for master views  which should be accessed from views.</p>\n'}, {'answer_id': 78658, 'author': 'Matt Mitchell', 'author_id': 364, 'author_profile': 'https://Stackoverflow.com/users/364', 'pm_score': 2, 'selected': False, 'text': '<p>I find that a common parent for all model objects you pass to the view is exceptionally useful.</p>\n\n<p>There will always tend to be some common model properties between pages anyway.</p>\n'}, {'answer_id': 746011, 'author': 'Generic Error', 'author_id': 40944, 'author_profile': 'https://Stackoverflow.com/users/40944', 'pm_score': 7, 'selected': True, 'text': '<p>If you prefer your views to have strongly typed view data classes this might work for you.  Other solutions are probably more <em>correct</em> but this is a nice balance between design and practicality IMHO.</p>\n\n<p>The master page takes a strongly typed view data class containing only information relevant to it:</p>\n\n<pre><code>public class MasterViewData\n{\n    public ICollection&lt;string&gt; Navigation { get; set; }\n}\n</code></pre>\n\n<p>Each view using that master page takes a strongly typed view data class containing its information and deriving from the master pages view data:</p>\n\n<pre><code>public class IndexViewData : MasterViewData\n{\n    public string Name { get; set; }\n    public float Price { get; set; }\n}\n</code></pre>\n\n<p>Since I don\'t want individual controllers to know anything about putting together the master pages data I encapsulate that logic into a factory which is passed to each controller:</p>\n\n<pre><code>public interface IViewDataFactory\n{\n    T Create&lt;T&gt;()\n        where T : MasterViewData, new()\n}\n\npublic class ProductController : Controller\n{\n    public ProductController(IViewDataFactory viewDataFactory)\n    ...\n\n    public ActionResult Index()\n    {\n        var viewData = viewDataFactory.Create&lt;ProductViewData&gt;();\n\n        viewData.Name = "My product";\n        viewData.Price = 9.95;\n\n        return View("Index", viewData);\n    }\n}\n</code></pre>\n\n<p>Inheritance matches the master to view relationship well but when it comes to rendering partials / user controls I will compose their view data into the pages view data, e.g.</p>\n\n<pre><code>public class IndexViewData : MasterViewData\n{\n    public string Name { get; set; }\n    public float Price { get; set; }\n    public SubViewData SubViewData { get; set; }\n}\n\n&lt;% Html.RenderPartial("Sub", Model.SubViewData); %&gt;\n</code></pre>\n\n<p><em>This is example code only and is not intended to compile as is.  Designed for ASP.Net MVC 1.0.</em></p>\n'}, {'answer_id': 948875, 'author': 'Michael La Voie', 'author_id': 65843, 'author_profile': 'https://Stackoverflow.com/users/65843', 'pm_score': 4, 'selected': False, 'text': '<p><strong>EDIT</strong></p>\n\n<p><a href="https://stackoverflow.com/users/40944/generic-error">Generic Error</a> has provided a better answer below.  Please read it!</p>\n\n<p><strong>Original Answer</strong></p>\n\n<p>Microsoft has actually posted an entry on <a href="http://www.asp.net/LEARN/mvc/tutorial-13-cs.aspx" rel="nofollow noreferrer">the "official" way</a> to handle this.  This provides a step-by-step walk-through with an explanation of their reasoning.</p>\n\n<p>In short, they recommend using an abstract controller class, but see for yourself.</p>\n'}, {'answer_id': 1496250, 'author': 'rasx', 'author_id': 22944, 'author_profile': 'https://Stackoverflow.com/users/22944', 'pm_score': 0, 'selected': False, 'text': '<p>The other solutions lack elegance and take too long. I apologize for doing this very sad and impoverished thing almost an entire year later:</p>\n\n<pre><code>&lt;script runat="server" type="text/C#"&gt;\n    protected override void OnLoad(EventArgs e)\n    {\n        base.OnLoad(e);\n        MasterModel = SiteMasterViewData.Get(this.Context);\n    }\n\n    protected SiteMasterViewData MasterModel;\n&lt;/script&gt;\n</code></pre>\n\n<p>So clearly I have this static method Get() on SiteMasterViewData that returns SiteMasterViewData.</p>\n'}, {'answer_id': 3444472, 'author': 'Todd Menier', 'author_id': 62600, 'author_profile': 'https://Stackoverflow.com/users/62600', 'pm_score': 6, 'selected': False, 'text': '<p>I prefer breaking off the data-driven pieces of the master view into partials and rendering them using <strong>Html.RenderAction</strong>. This has several distinct advantages over the popular view model inheritance approach:</p>\n\n<ol>\n<li>Master view data is completely decoupled from "regular" view models. This is composition over inheritance and results in a more loosely coupled system that\'s easier to change.</li>\n<li>Master view models are built up by a completely separate controller action. "Regular" actions don\'t need to worry about this, and there\'s no need for a view data factory, which seems overly complicated for my tastes.</li>\n<li>If you happen to use a tool like <a href="http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/01/22/automapper-the-object-object-mapper.aspx" rel="noreferrer">AutoMapper</a> to map your domain to your view models, you\'ll find it easier to configure because your view models will more closely resemble your domain models when they don\'t inherit master view data.</li>\n<li>With separate action methods for master data, you can easily apply output caching to certain regions of the page. Typically master views contain data that changes less frequently than the main page content.</li>\n</ol>\n'}]
['https://Stackoverflow.com/questions/78548', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/347616/']