0

I want to dynamically bind the menu item list in the master page depending upon the user login id. I'm new to MVC pattern where I get more confused by referring many sites. It will be helpful for me if u guide me to create dynamic menu. Here are the code which I have tried my best,

<script type="text/javascript">
    $(document).ready(function () {
        $("#accordian h3").click(function () {
            $("#accordian ul ul").slideUp();
            if (!$(this).next().is(":visible")) {
                $(this).next().slideDown();
            }
        });
    });
</script>

<nav class="navbar-default navbar-static-side" role="navigation">
<div id="accordian">
    <ul class="nav" id="side-menu">
        <li class ="nav-header">
                      @{
                foreach (var MenuItem in Model.MainMenuModel)
                {

                    var SubMenuItem = Model.SubMenuModel.Where(m => m.MainMenuID == MenuItem.ID);

                    <h3><a href="@MenuItem.MainMenuURL"> @MenuItem.MainMenuItem </a></h3>

                    if (SubMenuItem.Count() > 0)
                    {
                        <ul>
                            @foreach (var SubItem in SubMenuItem)
                            {
                                <li><a href='@SubItem.SubMenuURL'>@SubItem.SubMenuItem</a></li>
                            }
                        </ul>
                    }

                }
            }

I have business, data and services component in my project. From where I have to start to declare the menu items. Thanks in advance.

2
  • I don't see where you making menu choices based upon the user ID. Commented May 19, 2015 at 10:44
  • I missed that.. I'm passing it through session in javascript and receive it in the controllers. My doubt is that how will I receive the menu from the database to the view page? Commented May 19, 2015 at 11:22

1 Answer 1

1

I think it would be easiest to use @Html.Action() to render your menu passing your user id(or reading it in your action - for example from session - depending where you store such information).

With this approach you have one file where you place your HTML and JS, it is easy to pass a model to it from anywhere(since you just render your action you don't have to attach anything specific to other models or ViewBag/ViewData.

Possible functionality could be like:

Master

@Html.Action("Render", "Menu")

Your action

[ChildActionOnly]
public PartialViewResult Render() {
var userId = Session["some_key"];
var model = _userMenuService.GetModelForUserId(userId);
return PartialView(model);
}

You should get the point :)

EDIT:

To map your data to view you have to provide some view model, which can accept some IEnumerable<MenuItem> collection in constructor, which will be reflected to specific view model properties or you can just pass collection of all menu items to view and use them as your model.

Sign up to request clarification or add additional context in comments.

3 Comments

As you mentioned I will pass the user id as the session value. how do i return the menu values from the data repository to the view page?
You have to map your data to some view model, I edited my answer to reflect this.
Menu loads every time when Master(Layout.cshtml) refresh. How to make that menu to load only once?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.