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?
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?
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.
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.
[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);
}
...
}
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>
_Layout.cshtml
call on ActionThis 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?
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.
@Intruder Exactly. The idea is to terminate the scope of Action so she can’t be called in strange ways.
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 c# asp.net-mvc razor
You are not signed in. Login or sign up in order to post.
if vc declare a viewbag in all controler vc can use in the layout
– user3042136
What you want to display with Viewbag?
– Maicon Carraro
A list to fill a dropdow
– Paulo Henrique
I could tell you more or less what that list would look like?
– joaoeduardorf
would be a List<Selectlistitem> filled with an object
– Paulo Henrique
Where does this list come from?? An Entity Framework?
– joaoeduardorf
I fill it with the data I take manually from the bank
– Paulo Henrique