我想要完整的代码从 mvc 中的数据库填充菜单

发布于 2024-12-13 01:38:04 字数 68 浏览 1 评论 0原文

我创建了一个带有数据库菜单的 mvc 应用程序。我想从第一次在母版页中加载菜单时开始渲染菜单。

请帮我..

I Created a mvc application with database menu. I want to render the menu from when it is loading first time in the master page.

please help me..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

定格我的天空 2024-12-20 01:38:04

据我从你的问题中了解到,你需要一个需要在每个页面上呈现的菜单。请按照下列步骤操作:

  1. 为您的菜单项创建一个控制器操作。如下所示:

    <前><代码> [ChildActionOnly]
    公共 ActionResult 样本ChildAction() {

    var model = _myMenuRepo.GetAll();
    返回视图(模型);
    }

  2. 为此操作创建一个视图。看起来像下面这样:

    <前><代码>@{
    布局=空;
    }

  3. 从布局中调用此操作。代码应该如下所示:

    ;

    菜单

    @Html.Action("sampleChildAction", 新的{控制器=“样本”} )

本文也可以帮助您:

http ://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx

对于进一步的实现,您可能需要考虑缓存这部分您的应用程序:

http://www.tugberkugurlu.com/archive/donut-hole-caching-in-asp-net-mvc-by-using-child-actions-and-outputcacheattribute

As far as I understand from you question, you need a menu which you need to render on every page. Follow these steps :

  1. Create a controller action for your menu item. Something like below :

      [ChildActionOnly]
      public ActionResult sampleChildAction() {
    
          var model = _myMenuRepo.GetAll();
          return View(model);
      }
    
  2. Create a view for this action. Looks something like below :

      @{
          Layout = null;
      }
    
      <ul>
          @foreach(var item in Model) { 
              <li>
                  <a href="@item.MyHref">@item.MyDesc</a>
              </li>
          }
      </ul>
    
  3. Call this action from you layout. Code should look like something this :

    <div>
        <h3>Menu</h3>
    
        @Html.Action("sampleChildAction", 
          new { controller = "Sample" }
        )
    </div>
    

This article also can help you :

http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx

For further implementations, you might wanna consider caching this part of your application :

http://www.tugberkugurlu.com/archive/donut-hole-caching-in-asp-net-mvc-by-using-child-actions-and-outputcacheattribute

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文