below is my model
public class security
{
public long id { get; set; }
public long user_id { get; set; }
public long submenu_id { get; set; }
public bool flag { get; set; }
}
it the flag is true
the user has access to submenu.
Currently what i am doing is . When the user logged in the details in this model will be pulled out and store in seperate session variable.
eg: - var c = db.security.where(m=> m.user_id == id).Orderby(id);
if(c.submenu_id == 1 && c.flag == true)
session["mem_add"] = "true";
else
session["mem_add"] = "false";
Then in the layout view will check this session variable and if true
the menu will display, but problem is there are 16 sub menus
and can increase it later.So I have to create seperate session variables for each submenu. Is there any better way to solve this problem?
Edited
@if(Session["mem_add"] == "true")
{
<li class="active"><a href="@Url.Action("Index", "UpdateDetail")">
<img src="@Url.Content("~/Images/Enrollments.png")" alt=""><span class="title"><b>
@Resources.Resources.UpdateMyDetail</b> </span> </a></li>
}
Your other option would be to create an extension to HtmlHleper, then you can use it to pass a model to your menu and let it generate an MvcHtmlString
I would suggest a view model approach to represent your menu items
Then when a user logs in, create a collection of menu items, call your method to get the
security
items for the user, and based on theflag
property, set theCanView
property for each menu. Finally store the collection inSession
Then create a child only method to generate your menu
and create a partial view
_Menu.cshtml
and finally in the layout page, use
Html.Action()
to call the method that generates the menu items