微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – Asp.net mvc3中的自定义助手

我有一个ASP.NET MVC3应用程序.我想要一个自定义工具栏,我希望在每个表单中显示.这个自定义工具栏可以有一个或多个动作链接.所以,我需要开发一个Custom Html帮助器,我可以像下面一样使用它;

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

这个自定义扩展将生成链接html,我将在我的表单上显示它.我有一个ToolBaraction类,我想得到List< ToolBaraction>来自@ Html.CustomToolBar.

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

  }

你能告诉我如何实现这个目标吗?如果你能指出适当的资源,那真的很棒..

非常感谢

问候..

解决方法

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

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());
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐