Sneha K

Sneha K

  • 1.2k
  • 527
  • 195.5k

How to set rights for DynamicallyLoadingMenus and buttonsMVC

Jun 18 2017 1:26 PM

Hi I want to know how to set rights for the Add Edit Delete Buttons and Dynamic loading menus in MVC5. Actually i am loading menu dynamically. Now i am confussed with settings rights for menus and buttons.


**My Controller**

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

namespace DynamicMenuLoading.Controllers
{
public class CallRegisterController : Controller
{
//
// GET: /CallRegister/
public ActionResult Details()
{
return View();
}
}
}


**My View**


<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Details</title>
</head>
<body>
<div>

<a href="/Country/AddNewCountry" class="btn btn-primary" style="float: right;">Add New Country</a>
</div>
</body>
</html>
**My user settings table.**
This is userSettings table. Here I set Add and edit and View button to be visible for sneha in Call register view . So if sneha login means the call register menu to be visible first. Then click that call register menu means add and edit button to be visible for sneha How i set this. Because i am loading and showing menu dynamically in Layout page.
**My model to load menus dynamically**
public class Menu
{
public Menu()
{
MenuItems = new List<MenuItem>();
SubMenuItems = new List<SubMenuviewmodel>();
}
public int Id { get; set; }
public string Name { get; set; }
public List<MenuItem> MenuItems { get; set; }
public List<SubMenuviewmodel> SubMenuItems { get; set; }
}
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; }
}
public class SubMenuviewmodel
{
public string SubMenu { get; set; }
public string ActionName { get; set; }
public string MainMenuName { get; set; }
public string ControllerName { get; set; }
public string subid { get; set; }
}
**My controller to load menu**
public ActionResult SubMenu()
{
var menu = new Menu();
var mainmenu = db.ApplicationNames.Select(e => new
{
uid = e.UID,
mainmenuname = e.ApplicationName1
}).ToList();
var menuitemlist = new List<MenuItem>();
for (var item = 0; item < mainmenu.Count();item++ )
{
var mainmenuitems = new MenuItem { Name = mainmenu[item].mainmenuname.ToString(),Id=mainmenu[item].uid };
menuitemlist.Add(mainmenuitems);
}
menu.MenuItems = menuitemlist;
//submenu
var submenu = db.SubAplicationNames.Select(e => new
{
submenuuid = e.SubAplicationNameID,
submenuname = e.SubMenuName,
submenuparentidd = e.ApplicationID,
mainmenuname=e.ApplicationName
}).ToList();
var submenulist = new List<SubMenuviewmodel>();
for (var item = 0; item < submenu.Count();item++ )
{
var submenuitemslist = new SubMenuviewmodel { SubMenu = submenu[item].submenuname.ToString(),subid=submenu[item].submenuparentidd.ToString()
,ControllerName="Home",ActionName="About" };
submenulist.Add(submenuitemslist);
}
menu.SubMenuItems = submenulist;
return View(menu);
}
**My view to show menu dynamically**
@model DynamicMenuLoading.Models.Menu
@{
}
@foreach (var MainMenu in Model.MenuItems)
{
<a href="#">
<span>@MainMenu.Name</span>
</a>
<ul class="nav nav-second-level collapse">
@foreach (var SubMenu in Model.SubMenuItems.Where(x => x.subid == MainMenu.Id.ToString()))
{
<ul>
<li><a href="@Url.Action(SubMenu.ActionName, SubMenu.ControllerName, new { Title = SubMenu.SubMenu })"><i class="fa fa-circle-o"></i>@SubMenu.SubMenu</a></li>
</ul>
}
</ul>
}}
**Layout page**
<ul>
<li>
@Html.Action("Submenu","Menulist");
</li>
</ul>
 
 

Now i created call register view and load the menus dynamically. Now i want to set the rights for person by using User Settings tables. I tried my level best to explain the issue.Any one understand my issue and help me to resolve this problem

Thanks.

 
 
 

Answers (2)