views:

295

answers:

1

Hi, I want to create a pagination helper. The only parameters that it needs are currentpage, pagecount and routename. However, I do not know if it is possible to use the return value of another html helper inside the definition of my html helper. I am referring specifically to Html.RouteLink. How can I go about doing something like this in the class definition

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;

          }
     }
}

Thank you.

+2  A: 

Generally, yes you can use the results of other Html Helper functions within your custom functions. The exception would be any that write directly to the response stream rather than returning a string value.

I've done this sort of thing several times myself, and it works just fine...here's a sample I just totally made up right now based on something I did that I don't have the code for handy right now:

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();
}
ckramer
Can you please post a sample for the above code, that would make it a lot more clear for me. Thanks.
Interfector
Your code is more or less right...html helpers are basically building strings that get inserted into the page markup, so you can do pretty much whatever you want...even insert comments and stuff if you like.
ckramer

related questions