Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi to all,
I want to generate dynamically my menu control.
Can anyone tell me how to create dynamic menu in MVC5 based on user role that is
defined in user table in sql server database.
Posted

1 solution

Fist Solution: Controlling a link based on permission

AuthorizeActionLink: This only renders a link in the menu if user has access to it. You can customize the code to print any kind of HTML in the place of anchor tag if user does not have access. Check this out[^].

For a more flexible solution, check this out[^].

Second Solution: Multiple links at once for a dropdown menu

For a dropdown menu, best way to implement would be to create a model:

C#
public class MenuLink
{
    public string Name { get; set; }
    public string Link { get; set; }
    public List<menulink> Children { get; set; }
}
</menulink>


You can then populate children based on the links that have permissions. If no links have permission, then you can decide to hide that dropdown menu altogether. You can rework the code from here[^] to populate the list based on permission.

Hopefully this gets you started. Let me know if you have any questions.

Third Solution: Google

Upon a Google Search[^] I found good solutions. Check them out, see if they help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900