2010-06-13 53 views
1

我想創建一個分頁幫助器。它需要的唯一參數是currentpage,pagecount和routename。 但是,我不知道是否可以在我的html助手的定義中使用另一個html助手的返回值。我特指Html.RouteLink。我該怎麼做才能在類定義中做這樣的事情ASP.NET MVC 2中的自定義HTML幫助器

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; 

      } 
    } 
} 

謝謝。

回答

3

一般來說,是的,您可以在自定義函數中使用其他Html Helper函數的結果。例外情況是直接寫入響應流而不是返回字符串值的任何內容。

我已經做了幾次我自己的這種事情,它的工作原理很好...這裏是一個示例,我現在完全基於我做的事情,我沒有適當的代碼現在:

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

請問您可以發佈一個上面的代碼樣本,這會讓我更加清楚。謝謝。 – Interfector 2010-06-13 16:00:50

+0

你的代碼或多或少是正確的... html助手基本上是構建插入到頁面標記中的字符串,所以你可以做任何你想做的事情......甚至插入評論和東西,如果你喜歡。 – ckramer 2010-06-13 16:18:23

相關問題