自定义助手在Asp.net MVC3 [英] Custom Helper in Asp.net mvc3

查看:78
本文介绍了自定义助手在Asp.net MVC3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET MVC3应用程序。我想有我想要显示在每一个表单自定义工具栏。这个定制工具栏可以有一个或多个动作links.So,我需要开发一个自定义的HTML帮助,我可以使用如下;

I have an ASP.NET MVC3 application. I would like to have a custom toolbar that I want to display in every form. This custom toolbar can have one or many action links.So, I need to develop a Custom Html helper that I can use like below;

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

这个自定义扩展将产生HTML链接,我会显示它的窗体上。我有一个 ToolBarAction 类,我想获得列表< ToolBarAction> @Html .CustomToolBar

This custom extension will produce the links html and I will display it on my form. I have a ToolBarAction class and I would like to get List<ToolBarAction> from @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; }

  }

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

Can you advise me how I can achieve this? If you could point me the appropriate resources, that would be great really..

非常感谢

问候..

推荐答案

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

Something like this (I didn't know what the Name property was for, so I added it as the class):

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

这篇关于自定义助手在Asp.net MVC3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆