Pass Viewbag to _Layout

Asked

Viewed 1,096 times

2

I need to pass a list to my layout where it is used in all my pages, I would like to know how I could do this?

  • if vc declare a viewbag in all controler vc can use in the layout

  • What you want to display with Viewbag?

  • A list to fill a dropdow

  • I could tell you more or less what that list would look like?

  • would be a List<Selectlistitem> filled with an object

  • Where does this list come from?? An Entity Framework?

  • I fill it with the data I take manually from the bank

Show 2 more comments

3 answers

5


If the code is used on all pages, it is possibly a dynamic layout or menu. Do not use Session for that reason.

I will pass a list of steps to create a dynamic menu.

1. Create a Controller common

Create a file called Controller.cs in the directory Controllers of your project with the following:

public abstract class Controller : System.Web.Mvc.Controller
{

}

This ensures that all Controllers of its application are derived from this.

2. Create Actions with the Attribute [ChildActionOnly]

The Attribute ensures that the Action can only be called by another, and never directly.

Example:

public abstract class Controller : System.Web.Mvc.Controller
{
    ...
    [ChildActionOnly]
    public ActionResult Menu()
    {
        var menu = contexto.EntidadeDoMenu.ToList();

        return PartialView(menu);
    }
    ...
}

3. Create a View for each Action maid in the Controller common in the directory Views\Shared

An example (file Views\Shared\Menu.cshtml):

@model IEnumerable<MeuProjeto.Models.ItemDeMenu>

<ul>
    @foreach (var itemDeMenu in Model) { 
       <li>@Html.ActionLink(itemDeMenu.Nome, "Detalhes", "UmControllerQualquer", new { id = itemDeMenu.Id }, null)</li>
    }
</ul>

4. Do _Layout.cshtml call on Action

This step is optional. You can make another component call the Action.

@Html.Action("Menu")
  • The Menu View (Views Shared Menu.cshtml file) needs to be a partial view?

  • 1

    Thanks for the reply, I will try to implement here, anything I return.

  • Responding to myself, I saw in the code that you return a Partialview, explicitly.

  • 1

    @Intruder Exactly. The idea is to terminate the scope of Action so she can’t be called in strange ways.

  • 1

    I did it this way and it worked, thank you very much.

0

I think it’s kind of like what you want:

List<SelectListItem> listItem = new List<SelectListItem>();
List<SeuModel> listModel = repositorio.BuscarDados();

foreach(var item in listModel)
{
   listItem.Add({
      Text: item.Descricao,
      Value: item.Id
   });
}

ViewBag.seuDropDownList = listItem;

I’m sorry if there are errors in the code, I’m not with the dlls and components to test.

  • I am already able to fill the Viewbag with this list, I’m having trouble using this Viewbag in _Layout, I don’t know in which controller I should pass this information.

0

The @Gypsy should probably know a better solution, but the only thing I thought was to add on own Session, example:

var selectList = new SelectList (suaLista, "ID", "Texto");
Session.Add("meudropdown", selectList);

And in the _Layout

@Html.DropDownList("meudrop", Session["meudropdown"] as SelectList)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.