代码之家  ›  专栏  ›  技术社区  ›  Interfector

ASP.NET MVC 2中的自定义HTML帮助程序

  •  1
  • Interfector  · 技术社区  · 16 年前

    我想创建分页助手。它只需要currentpage、pagecount和routename参数。 但是,我不知道是否可以在html助手的定义中使用另一个html助手的返回值。我指的是Html.RouteLink。我怎样才能在类定义中这样做呢

    using System;
    using System.Web.Mvc;
    
    namespace MvcApplication1.Helpers
    {
         public static class LabelExtensions
         {
              public static string Label(this HtmlHelper helper, string routeName, int currentpage, int totalPages)
              {
                   string html = "";
                   //Stuff I add to html
                   //I'd like to generate a similar result as the helper bellow. 
                   //This code does not work, it's just an example of what I'm trying to accomplish
                   html .= Html.RouteLink( pageNo, routeName, new { page = pageNo - 1 } );
                   //Other stuff I do the html
                   return html;
    
              }
         }
    }
    

    非常感谢。

    1 回复  |  直到 16 年前
        1
  •  3
  •   ckramer    16 年前

    通常,您可以在自定义函数中使用其他Html助手函数的结果。例外情况是任何直接写入响应流而不是返回字符串值的操作。

    我自己也做过几次这样的事情,而且效果很好……这是一个我刚刚完全根据我做的事情制作的示例,我现在没有现成的代码:

    public static string RssFeed(this HtmlHelper helper, string url)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append(GetRSSMarkup(url));  // This generates the markup for the feed data
        sb.Append(helper.ActionLink("Go Home","Index","Home"));
        return sb.ToString();
    }