Skip to content

Instantly share code, notes, and snippets.

@willnss
Created November 20, 2011 11:43
Show Gist options
  • Save willnss/1380185 to your computer and use it in GitHub Desktop.
Save willnss/1380185 to your computer and use it in GitHub Desktop.
Exemplo Menu básico reutilizável para ASP.NET MVC - ASPXViewEngine
//MvcApplicationFolder\Views\Shared\Site.Master
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<!DOCTYPE html>
<html>
<head></head>
<body>
<!--Local onde será renderizado o menu-->
<%:Html.Action("Menu", "Home") %>
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
</body>
</html>
//MvcApplicationFolder\Controllers\HomeController.cs
public class HomeController : Controller {
//Outras actions
//ChildActionOnly garantirá que esta action não poderá ser invocada pela url mas somente como parcial através de Html.RenderAction(...) e Html.Action(...)
[ChildActionOnly]
public ActionResult Menu()
{
List<MenuItemModel> menu = new List<MenuItemModel>();
menu.Add(new MenuItemModel { Texto = "Item 1", Id = "2" });
menu.Add(new MenuItemModel { Texto = "Item 2", Id = "3" });
menu.Add(new MenuItemModel { Texto = "Outro Item", Id = "4" });
return View(menu);
}
public ActionResult Categorizar(string Id)
{
ViewBag.Id_Categoria = Id;
return View();
}
}
//MvcApplicationFolder\Views\Shared\Menu.ascx
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<MvcApplicationFolder.Models.MenuItemModel>>" %>
<ul id="menu">
<% foreach (var item in Model) { %>
<li>
<!--Categorizar será o nome da Action e Home "HomeController" o controller-->
<%: Html.ActionLink(item.Texto, "Categorizar", "Home", new { Id = item.Id }, null) %>
</li>
<% } %>
</ul>
//MvcApplicationFolder\Models\MenuItemModel.cs
public class MenuItemModel
{
public string Texto { get; set; }
public string Id { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment