gpt4 book ai didi

c# - ASP.Net MVC3 Razor 中的动态菜单加载

转载 作者:太空宇宙 更新时间:2023-11-03 10:52:37 25 4
gpt4 key购买 nike

我想问一下在 ASP.Net 中加载动态数据库驱动的菜单

MVC3 Razor 。

这是我的

1.查看代码

2.菜单模型

3.菜单项模型

4.导航模型结构

在这个项目中,我有不同的用户登录名。对于每个登录名,它们都是

受模块限制。所以当用户使用他们的

进入应用程序时

登录,他们对其帐户的访问权限有限。

For Example,There are two users A and B. User A can access only Accounts and Reports and User B can access only Reports .

为了实现这些东西,我从数据库加载菜单。

我的问题是如何在ASP.Net MVC3 Razor中实现从数据库加载菜单

查看代码

@{
ViewBag.Title = "Elixir ERP V1.0 Beta";
Layout = "~/Views/Shared/_LayoutUser.cshtml";
}
<div class="main-container">
<div class="main-wrapper">
<div class="scroll-top">
<a href="#" class="tip-top" title="Go Top"><i

class="icon-arrow-up"></i></a>
</div>
<div class="left-bar merge-left">
<!-- SEARCH BAR -->
<!-- LEFT NAV -->
@section leftnav{


}
</div>
</div>
<div class="container">
</div>
</div>

菜单模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Elixir.Models
{
public class Menu
{
public Menu()
{
MenuItems = new List<MenuItem>();
}

public int Id { get; set; }
public string Name { get; set; }
public List<MenuItem> MenuItems { get; set; }
}
}

菜单项

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Elixir.Models
{
public class MenuItem
{

public int Id { get; set; }
public int UserName { get; set; }
public string Name { get; set; }
public string ActionName { get; set; }
public string ControllerName { get; set; }
public string Url { get; set; }
public Menu ParentMenu { get; set; }
}
}

导航模型结构

 <div class="left-nav">


<ul class="side-navigation accordion"

id="nav-accordion">
<li><a href="#"><i

class="icon-home"></i>Home</a></li>
<li><a href="#"><i class="icon-list-alt"></i>User

Management</a>
<ul>
<li><a href="#"><i

class="icon-double-angle-right"></i>Create New User</a></li>
<li><a href="#"><i

class="icon-double-angle-right"></i>Manage Users</a></li>
<li><a href="#"><i

class="icon-double-angle-right"></i>View Users</a></li>
</ul>
</li>
<li><a href="#"><i

class="icon-table-2"></i>Employee Management</a>
<ul>
<li><a href="#"><i

class="icon-double-angle-right"></i>Employee Registration</a></li>
<li><a href="#"><i

class="icon-double-angle-right"></i>Manage Employees</a></li>
<li><a href="#"><i

class="icon-double-angle-right"></i>View Employees</a></li>
</ul>
</li>
<li><a href="#"><i class="icon-columns"></i>DMIT

Management</a>
<ul>
<li><a href="#"><i

class="icon-double-angle-right"></i>Scan Management</a></li>
<li><a href="#"><i

class="icon-double-angle-right"></i>Report Generation</a></li>
</ul>
</li>
<li><a href="#"><i

class="icon-laptop"></i>Franchise Management </a>
<ul>
<li><a href="#"><i

class="icon-double-angle-right"></i>Franchise Registration</a></li>
<li><a href="#"><i

class="icon-double-angle-right"></i>Franchise Fee Management</a></li>
<li><a href="#"><i

class="icon-double-angle-right"></i>Franchise Account

Management</a></li>
</ul>
</li>
<li><a href="#"><i class="icon-font"></i>Customer

Management </a>
<ul>
<li><a href="#"><i

class="icon-double-angle-right"></i>Customer Registration</a></li>
<li><a href="#"><i

class="icon-double-angle-right"></i>Manage Customer</a></li>
</ul>
</li>
<li><a href="#"><i

class="icon-cord"></i>Counseling Management</a>
<ul>
<li><a href="#"><i

class="icon-double-angle-right"></i>Add Counseling Session</a></li>
<li><a href="#"><i

class="icon-double-angle-right"></i>Manage Counseling

Sessions</a></li>
</ul>
</li>
<li><a href="#"><i

class="icon-tools"></i>Accounts</a>
<ul>
<li><a href="#"><i

class="icon-double-angle-right"></i>Manage Income</a></li>
<li><a href="#"><i

class="icon-double-angle-right"></i>Manage Expense</a></li>
</ul>
</li>
<li><a href="#"><i

class="icon-files"></i>Reports</a>
<ul>
<li><a href="#"><i

class="icon-double-angle-right"></i>Elxir Monthly Reports</a></li>
<li><a href="#"><i

class="icon-double-angle-right"></i>Elixir Yearly Reports</a></li>
<li><a href="#"><i

class="icon-double-angle-right"></i>Franchise Monthly

Reports</a></li>
<li><a href="#"><i

class="icon-double-angle-right"></i>Franchise yearly Reports</a></li>
</ul>
</li>
</ul>
</div>

最佳答案

放在_Shared.cshtml中的div下面

  <nav id="mainNavigation">
@Html.Partial("_MenuLayout")
</nav>

为动态生成的菜单创建新的_MenuLayout.cshtml View

在您的 Controller 中,编写代码以从数据库表中获取数据,例如MenuTable 并准备列表并分配值,如下所示

  ViewBag.Menu = list;

并在您的 _MenuLayout.cshtml 部分 View 中代码下方,

@{ var menusList = ViewBag.Menu as IEnumerable<Elixir.Models.MenuItem>; }

@if (menusList != null)
{
<ul id="menu" class="wrap cf">
@foreach (var parentMenu in menusList.Where(p => p.ParentMenuID == 0))
{
<li>
@if (!string.IsNullOrEmpty(@parentMenu.ActionName))
{
@Html.ActionLink(@parentMenu.MenuName, @parentMenu.ActionName, @parentMenu.ControllerName)
}
else
{
<span>@parentMenu.MenuName</span>
if (menusList.Count(p => p.ParentMenuID == parentMenu.MenuID) > 0)
{
<ul>
@foreach (var childMenu in menusList.Where(p => p.ParentMenuID == parentMenu.MenuID))
{
<li>@Html.ActionLink(@childMenu.MenuName, @childMenu.ActionName, @childMenu.ControllerName)</li>

if (menusList.Count(p => p.ParentMenuID == childMenu.MenuID) > 0)
{
foreach (var subChild in menusList.Where(p => p.ParentMenuID == childMenu.MenuID))
{
@Html.ActionLink(@subChild.MenuName, @subChild.ActionName, @subChild.ControllerName)
}
}
}
</ul>
}
}

</li>
}
</ul>
}

关于c# - ASP.Net MVC3 Razor 中的动态菜单加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20865861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com