且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

自定义助手在Asp.net MVC3

更新时间:2022-12-30 21:17:12

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

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