tags:

views:

774

answers:

4

I have several pages listing search results, for each result I would like to display I want to create a custom View Helper in order to avoid duplicating the display code.

How do I access the convenient existing view helpers from my custom view helper? I.e. in my custom view helper I would like to use Url.Action(), Html.ActionLink, etc. How do I access them from my custom view helper?

using System;
namespace MvcApp.Helpers
{
    public class SearchResultHelper
    {
        public static string Show(Result result)
        {
            string str = "";

            // producing HTML for search result here

            // instead of writing
            str += String.Format("<a href=\"/showresult/{0}\">{1}</a>", result.id, result.title);
            // I would like to use Url.Action, Html.ActionLink, etc. How?

            return str;
        }
    }
}

using System.Web.Mvc gives access to HtmlHelpers, but non of the convenient methods like ActionLink seem to be present.

+4  A: 

This example should help you. This helper renders different link text depending on whether the user is logged in or not. It demonstrates the use of ActionLink inside my custom helper:

    public static string FooterEditLink(this HtmlHelper helper,
        System.Security.Principal.IIdentity user, string loginText, string logoutText)
    {
        if (user.IsAuthenticated)
            return System.Web.Mvc.Html.LinkExtensions.ActionLink(helper, logoutText, "Logout", "Account",
                new { returnurl = helper.ViewContext.HttpContext.Request.Url.AbsolutePath }, null);
        else
            return System.Web.Mvc.Html.LinkExtensions.ActionLink(helper, loginText, "Login", "Account",
                new { returnurl = helper.ViewContext.HttpContext.Request.Url.AbsolutePath }, null);
    }

EDIT:
All you would need to do to to access the Url.Action() method would be to replace the this HtmlHelper helper param with something like this UrlHelper urlHelp and then just call urlHelp.Action(...

Hope this helps.

cottsak
A: 

In my opinion, you shouldn't be trying to use ActionLink within code. The whole concept of MVC is to separate logic from display, so you should try to stick with that.

I would suggest you pass the result object through to the view (maybe through ViewData) and then parse the result inline within the view. e.g.

<%= Html.ActionLink(result.title,"/showresult/" + result.id, "myController") %>
ZombieSheep
I see your point and agree. However, that would mean I need to duplicate the parse/display code/logic in several places, which is what I try to avoid.
stefpet
There's no good reason why one html helper can't call another.
Iain Galloway
A: 

A simple gravatar html helpler, your class needs to be static also.

  public static string GetGravatarURL(this HtmlHelper helper, string email, string size, string defaultImagePath)
    {

        return GetGravatarURL(email, size) + string.Format("&default={0}", defaultImagePath);

    }
Skiltz
A: 

you can extend the default HtmlHelper and UrlHelper just with an extension method (so you have the xxxHelper as first param in your method).

Or you can just create your base view with the method you want and use the Html or URL variable of the view.

Andrea Balducci