我想问一下ASP.Net中的动态数据库驱动菜单加载
MVC3剃须刀。
这是我的
1.查看代码
2.菜单模型
3.菜单项的模型
4.导航模型结构
在这个项目中,我有不同的用户登录。对于每次登录,他们都是
受模块限制。所以当用户使用
进入应用程序时 登录后,他们只能访问自己的帐户。例如,有两个用户A和B.用户A只能访问帐户和报告,用户B只能访问 报告。
要实现这些功能,请从数据库加载菜单。
我的问题是 如何实现菜单从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>
答案 0 :(得分:2)
将下面的div放在_Shared.cshtml
中 <nav id="mainNavigation">
@Html.Partial("_MenuLayout")
</nav>
为动态生成的菜单创建新的_MenuLayout.cshtml
视图
在您的控制器中,编写代码以从数据库表中获取数据,例如MenuTable并准备列表并分配如下所示的值
ViewBag.Menu = list;
并在代码下方的_MenuLayout.cshtml
部分视图中
@{ 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>
}