This is a migrated thread and some comments may be shown as answers.

Dynamic Menu Items

2 Answers 213 Views
Menu
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Kent Muller
Top achievements
Rank 1
Kent Muller asked on 31 Mar 2010, 02:24 AM
Hi, I am using MVC Menu and would like to dynamically create some menu items from the controller, while still using standard menu construction:  Example below:

<% Html.Telerik().Menu() 
        .HtmlAttributes(new { style"border: 0;" }) 
        .Name("Menu") 
        .Items(menu => 
        { 
            menu.Add() 
            .HtmlAttributes(new { style = "border-right: 0;" }) 
                .Text("Process") 
                .Content(() => 
                    {%> 
                        <!-- create dynamic menus in controller --> 
                    <%}); 
            menu.Add() 
                .HtmlAttributes(new { style = "border-right: 0;" }) 
                .Text("Print") 
                .Items(item => 
                { 
                    item.Add().Text("Welcome Letter") 
                        .Action("PrintWelcomeLetter", "Services", new { id = ViewData["ClientProcessID "] }) ; 
                    item.Add().Text("Application Form") 
                        .Action("PrintApplicationForm", "Services", new { id = ViewData["ClientProcessID "] }) ; 
                }); 
            menu.Add() 
                .HtmlAttributes(new { style = "border-right: 0;" }) 
                .Text("Attachments") 
                .Items(item => 
                { 
                    item.Add() 
                        .Text("Add Attachment") 
                        .Action("AddServiceAttachment", "Services", new { id = ViewData["ClientProcessID"] }); 
                }); 
        }) 
        .Render(); 
%> 

Based on route data I would be wanting to output different menu options which allow the user to move to the next stage in the process.  
I assume it is best to do this with the Content feature using ViewData, however being quite new to this I would appreciate some assistance in how to construct the menu items in the controller dynamically.

I'm using MVC 1 with C#, any assistance would be greatly appreciated.

Many Thanks,

Kent.

2 Answers, 1 is accepted

Sort by
0
Peter
Top achievements
Rank 1
answered on 31 Mar 2010, 09:12 AM
Hi,

It will much harder to achieve your goal using Content method. I am sure, because the Content method will output pure HTML. Hence you cannot just add MenuItem objects and to expect output menu items with the Content method. You can review this help article.

So you can accomplish your task as you pass the required information to the View (using ViewData). Once you have the required info you can just loop it in the Items method and add the required items. Here is a simple code snippet:
            menu.Add()  
            .HtmlAttributes(new { style = "border-right: 0;" })  
                .Text("Process"
                .Items(items => 
                       for(int i=0; i < items count; i++) 
                       { 
                          items.Add()... and the required info. 
                       } 
                 ) 

Regards,
Peter
0
Osama A. Kadir
Top achievements
Rank 1
answered on 04 Jul 2010, 06:22 PM
I try Deploy Nased Menu, it's work when i running using Visual Studio. But when I Use IIS 5.1 and IIS6 the menu not genereted. have an Idea and what I must do to solve it ? (this menu on ascx file and I am Using MVC 2 with C#)

below the code :
<% if (Model != null) 
    Html.Telerik().Menu() 
        .HtmlAttributes(new { style = "border-right: 0;" }) 
        .Name("MenuPrevilage") 
        .Effects(fx => fx.Slide() 
                         .Opacity() 
                         .OpenDuration(200) 
                         .CloseDuration(200) 
        )            
        .Items(items => 
        { 
            var level0 = Model.Where(s => s.ModuleLevel == 0).ToList<NavigationData>();             
            for (int i = 0; i < level0.Count; i++) 
            { 
                string idModule = level0[i].IdModule; 
                items.Add() 
                 .Text(level0[i].ModuleName) 
                 .Url(level0[i].ModuleUrl != null ? level0[i].ModuleUrl : "/#") 
                 .Items(item => 
                 { 
                     var level1 = Model.Where(x => x.IdParent == idModule).OrderBy(y => y.Increment).ToList<NavigationData>(); 
                     for (int j = 0; j < level1.Count; j++) 
                     { 
                         item.Add() 
                         .Url(level1[j].ModuleUrl != null ? level1[j].ModuleUrl : "/#") 
                         .Text(level1[j].ModuleName.ToString()); 
                     } 
                 });                 
            } 
        }) 
        .Render(); 
%> 
 
 

Tags
Menu
Asked by
Kent Muller
Top achievements
Rank 1
Answers by
Peter
Top achievements
Rank 1
Osama A. Kadir
Top achievements
Rank 1
Share this question
or