<p>Here is a scenario: Let's say I have site with two controllers responsible for displaying different type of content - Pages and Articles. I need to embed Partial View into my masterpage that will list pages and articles filtered with some criteria, and be displayed on each page. I cannot set Model on my masterpage (am I right?). How do I solve this task using Html.RenderPartial?</p>

<p>[EDIT]
Yes, I'd probably create separate partial views for listing articles and pages, but still, there is a barrier that I cannot and shouldn't set model on masterpage. I need somehow to say "here are the pages" as an argument to my renderpartial, and also for articles. Entire concept of renderpartial with data from database in masterpages is a bit blurry to me.</p>

[{'answer_id': 48821, 'author': 'Ben Scheirman', 'author_id': 3381, 'author_profile': 'https://Stackoverflow.com/users/3381', 'pm_score': 0, 'selected': False, 'text': '<p>The ViewData Model property should only be used for the content that you\'re viewing/editing on the main section of the UI.</p>\n\n<p>Other parts of the view may need some data present in ViewData, so just add those to the dictionary.</p>\n\n<p>I\'d just pass data from the dictionary like this:  ViewData["articles"] to the partial.  (or ViewData.Get() from MvcContrib).</p>\n\n<p>You might also look at the recently implemented SubController pattern implemented in <a href="http://mvccontrib.org" rel="nofollow noreferrer">MvcContrib</a>.</p>\n'}, {'answer_id': 48827, 'author': 'Dragan Panjkov', 'author_id': 481, 'author_profile': 'https://Stackoverflow.com/users/481', 'pm_score': 0, 'selected': False, 'text': '<p>yes, this is correct. but let\'s look at this scenario:\non views that are related to articles, I\'d have ViewData["article"], and on views related to pages, I have ViewData["pages"], but I don\'t have both articles and pages available all time. So, if I add:</p>\n\n<p>Html.RenderPartial("articlesView", ViewData["articles"])</p>\n\n<p>Html.RenderPartial("pagesView", ViewData["pages"])</p>\n\n<p>to my masterpage, I\'ll have an exception thrown on each page on which ViewDataDictionary doesn\'t contain both articles and pages.</p>\n\n<p>At least, that\'s how I see it.</p>\n'}, {'answer_id': 641339, 'author': 'Simon_Weaver', 'author_id': 16940, 'author_profile': 'https://Stackoverflow.com/users/16940', 'pm_score': 1, 'selected': False, 'text': '<p>I had a <a href="https://stackoverflow.com/questions/467855/should-an-asp-net-masterpage-get-its-data-from-the-view">similar post</a> and came up with an object model to handle it. </p>\n\n<p>I HATE the non-strongly typed views so went with this approach and it is working well.</p>\n'}, {'answer_id': 919625, 'author': 'user113588', 'author_id': 113588, 'author_profile': 'https://Stackoverflow.com/users/113588', 'pm_score': 2, 'selected': False, 'text': '<p>How about creating an HtmlHelper extension method that allows you to call a partial view result on the an action on the controller.</p>\n\n<p>Something like</p>\n\n<pre><code> public static void RenderPartialAction&lt;TController&gt;(this HtmlHelper helper, Func&lt;TController, PartialViewResult&gt; actionToRender)\n    where TController : Controller, new()\n{\n    var arg = new TController {ControllerContext = helper.ViewContext.Controller.ControllerContext};\n    actionToRender(arg).ExecuteResult(arg.ControllerContext);\n} \n</code></pre>\n\n<p>you could then use this in your master page like</p>\n\n<pre><code>&lt;% Html.RenderPartialAction((HomeController x) =&gt; x.RenderPartial()) %&gt;\n</code></pre>\n\n<p>and in your controller the appropriate method</p>\n\n<pre><code>public PartialViewResult RenderPartial()\n{\n\n     return PartialView("~/Path/or/View",_homeService.GetModel())\n}\n</code></pre>\n\n<p>Well that is my 2 cents anyway</p>\n'}, {'answer_id': 1727633, 'author': 'Jeremy', 'author_id': 52168, 'author_profile': 'https://Stackoverflow.com/users/52168', 'pm_score': 0, 'selected': False, 'text': '<p>The way that I handle this is by using a BaseViewModel.  All Views are strongly typed against a view model that inherits from BaseViewModel.  </p>\n\n<p>The BaseViewModel class has all of the information needed by the MasterPage.  So for navigation your BaseViewModel may look like this:</p>\n\n<pre><code>public class BaseViewModel\n{\n    public BaseViewModel()\n    {\n         NavigationItems = RetrieveNavigationItemsFromModel();\n    }\n    public List&lt;NavItems&gt; NavigationItems {get; set;}\n}\n</code></pre>\n\n<p>In your MasterPage and PartialViews, you can cast the Model to BaseViewModel and access the NavigationsItems property.</p>\n\n<pre><code>&lt;ul&gt;\n&lt;% foreach (NavItem ni in (Model as BaseViewModel).NavigationItems) { %&gt;\n    &lt;li&gt;\n        &lt;a href="&lt;%= ni.Url %&gt;" alt="&lt;%= ni.Alt%&gt;"&gt;&lt;%= ni.DisplayText %&gt;&lt;/a&gt;\n    &lt;/li&gt;\n&lt;% } %&gt;\n&lt;/ul&gt;\n</code></pre>\n'}, {'answer_id': 1932597, 'author': 'Brett Rigby', 'author_id': 114805, 'author_profile': 'https://Stackoverflow.com/users/114805', 'pm_score': 1, 'selected': False, 'text': '<p>I think that your solution may lie in the land of MVC 2.0 RC and beyond...</p>\n\n<p>Phil Haack posted an article on his blog: <a href="http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx" rel="nofollow noreferrer">http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx</a></p>\n'}, {'answer_id': 2026869, 'author': 'Ciddan', 'author_id': 68891, 'author_profile': 'https://Stackoverflow.com/users/68891', 'pm_score': 0, 'selected': False, 'text': '<p>This is a very late reply, but I got to this page whilst googling - so chances are someone else will see this question (and my reply) as well.</p>\n\n<p>The way I\'ve worked around this problem is by using a simple jQuery script to load a PartialView and execute it\'s controller code. Sample below.</p>\n\n<pre><code>&lt;asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server"&gt;\n\n    &lt;script type="text/javascript"&gt;\n            $(document).ready(function() {\n            $("#applicationForm").load("/Home/ApplicationForm");\n            });\n    &lt;/script&gt;\n\n    &lt;div id="applicationForm" /&gt;\n\n&lt;/asp:Content&gt;\n</code></pre>\n\n<p>The big drawback to this approach is that the client has to have scripting enabled for it to work (so it\'s really SEO unfriendly). If that\'s something you can live with it works well. I only use it on an intranet site where I know that each client has JavaScript enabled and I don\'t have to worry about google\'s bots.</p>\n'}]
['https://Stackoverflow.com/questions/48794', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/481/']