2011-09-10 105 views
4

我有一个ASP.NET MVC3应用程序。我想有一个自定义工具栏,我想在每种形式中显示。这个自定义工具栏可以有一个或多个操作链接。因此,我需要开发一个自定义Html帮助程序,我可以在下面使用它;在Asp.net中的自定义帮助mvc3

@Html.CustomToolBar(items => { 
      items.Add("Action","Controller","Name","Text"); 
      items.Add("Action1","Controller1","Name1","Text1");}) 

此自定义扩展将产生链接html,我会将其显示在我的表单上。我有一个ToolBarAction班,我想从@Html.CustomToolBar得到List<ToolBarAction>

public class ToolbarAction 
    { 
    public string Name { get; set; } 
    public string Action { get; set; } 
    public string Controller { get; set; } 
    public string Text { get; set; } 

    } 

你能告诉我怎么做到这一点吗?如果你可以点我相应的资源,那将是巨大真的..

非常感谢

问候..

回答

2

像这样的东西(我不知道什么名称属性是为,所以我添加它作为类):

public static class HelperExtensions 
{ 
    public static MvcHtmlString Menu(this HtmlHelper html, Action<IList<ToolbarAction>> addActions) 
    { 
     var menuActions = new List<ToolbarAction>(); 
     addActions(menuActions); 

     var htmlOutput = new StringBuilder(); 

     htmlOutput.AppendLine("<div id='menu'>"); 

     foreach (var action in menuActions) 
      htmlOutput.AppendLine(html.ActionLink(action.Text, action.Action, action.Controller, new { @class = action.Name }).ToString()); 

     htmlOutput.AppendLine("</div>"); 

     return new MvcHtmlString(htmlOutput.ToString()); 
    } 
} 
+0

非常感谢莫滕! – AnarchistGeek

+0

如何使用Menu函数来生成工具栏....没有共享代码:( – Thomas

+0

它与问题中的代码相同,除了我命名方法'Menu'而不是'CustomToolBar'。 –