A
A
anlamas2014-06-27 08:04:21
ASP.NET
anlamas, 2014-06-27 08:04:21

How to pass data for strongly typed partial view or for _Layout.cshtml?

Hello! On the site, I use a strongly typed partial view for navigation (navigation is rendered on all pages). I use master pages.
_Layout.cshtml

<body>
@{Html.RenderPartial("_Navigation");}
@RenderBody()
<body/>

_Navigation.cshtml
@model IEnumerable<ProgramManager.Models.Category>
<div class="list">
    @foreach (var item in Model) {
         <div class="list-item">
              @Html.ActionLink(item.Name, "Item", "Category", new { id = item.Id})
         </div>
    }
</div>

Here, now how (from where?) to transfer data to _Navigation.cshtml? Given that navigation appears on all pages of the site. Or is it possible to somehow pass it to _Layout.cshtml? then it will be global

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Valery Abakumov, 2014-07-03
@anlamas

Good afternoon!
By using
you simply display the view.
To pass the model to the view, you need to form this model. It is better to form the model in the action method , for example, like this:

[HttpGet]
public ActionResult Navigation()
{
  List<MyClass> classes = new List<MyClass>();
  classes.Add(new MyClass() { Id = 1, Name = "item 1" });
  classes.Add(new MyClass() { Id = 2, Name = "item 2" });
  classes.Add(new MyClass() { Id = 3, Name = "item 3" });
  
  return View("_Navigation", classes);
}

Now you need to replace
with this:
RenderPartial simply renders a view, while RenderAction roughly "requests a view from an action method".
Your second question
Or is it possible to somehow pass it to _Layout.cshtml? then it will be global
didn't understand.
Hope it helped you in some way.
Good luck!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question