How to pass data to a PartialView in my layout?


I have a _layout.cshtml containing this row:


Now I want to pass in a model into this RenderPartial-function. This model can be read from my repository.

How and where(in code) can this be done?



RenderPartial has an overload that can take an object to send it to the partial view. Don't forget to define your @model at the top of your partialview to work with the right object type.


Extra info: <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.html.renderpartialextensions.renderpartial.aspx" rel="nofollow">MSDN</a>

<strong>Edit after comment:</strong>

I think it would be easier to create a MenuController that takes in the repository. Then let it create a view which takes in the required repository as it's model, then with a foreach render every menu-item as actionlinks, passing the menu info to it.

So you would have this in your _layout.cshtml:

<div id="Menu"> @{Html.RenderAction("Menu", "Menu");} </div>

This in your MenuController:

public class MenuController : Controller { private IMenuRepository _repository; public MenuController(IMenuRepository repo) { _repository = repo; } // // GET: /Menu/ public PartialViewResult Menu(string menu = null) { ViewBag.SelectedMenu = menu; IEnumerable<MenuInfoObject> menus= _repository.Menus; return PartialView(menus); } }

And your MenuView:

@model IEnumerable<MenuInfoObject> @{ Layout = null; } @foreach (var item in Model) { @Html.RouteLink(item.MenuName, new { controller = item.ControllerInfo, action = item.ActionInfo, }, new { @class = item.Menu == ViewBag.SelectedMenu ? "selected" : null }) }

Would that be any closer to a solution?


There is another solution as well to pass data to a partial view in Layout. You can simply add this in your _Layout.cshtml file


And in your Controller :

[ChildActionOnly] public ActionResult ActionName() { var model = new YourModel(); return PartialView(model); }

The ChildActionOnly attribute ensures that an action method can be called only as a child method. This action will render the corresponding partial view with model in Layout.


  • SAPUI5 navigation between apps definitions
  • Adding a class if condition matches in Angular JS
  • what is laravel render() method for?
  • ember.js model data is not being output by the collectionView
  • How to create a view from Bigquery REST api
  • Unity High_Score Badges C#
  • Unit test express route calls controller method?
  • Exception handling as per java coding standards
  • Whats the right place for testhelper-classes? (phpunit/best practise)
  • SQL query to group by maximal sets of a column having inner consecutive distances below a threshold
  • IE6 changes DOCTYPE to a bad one
  • Extract All Possible Paths from Expression-Tree and evaluate them to hold TRUE
  • New Firebase failed: First argument must be a valid firebase URL and the path can't contain “.”
  • JSON encode and decode on PHP
  • Building Qt project for C++11 standard
  • XSLT foreach repeating nodes to flat
  • Azure webjobs output logs indexing taking very long
  • jQuery: add elements until a particular height is reached
  • List images(01.png) and descriptions(01.txt) from directory
  • Does Mobilefirst provide a provision to access web services directly?
  • Reduction and collapse clauses in OMP have some confusing points
  • Calling Worksheet functions from vba in foreign language versions of Excel
  • ADO and msqli connections very slow
  • Marklogic : Query response time is very high
  • Custom Tabgroup Appcelerator
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • D3 nodes and links from JSON with nested arrays of children
  • $wpdb not working in file of WordPress plugin
  • How to rebase a series of branches?
  • Projection media query: browser support and workarounds?
  • java.lang.NoClassDefFoundError: com.parse.Parse$Configuration$Builder on below Lollipop versions
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • R: gsub and capture
  • Arrays break string types in Julia
  • Comma separated Values
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Error creating VM instance in Google Compute Engine
  • Hits per day in Google Big Query
  • how does django model after text[] in postgresql [duplicate]
  • Does armcc optimizes non-volatile variables with -O0?