30

I am designing a custom HTML helper and I would like to execute Html.ActionLink to provide dynamic URL generation.

    namespace MagieMVC.Helpers
    {
        public static class HtmlHelperExtension
        {
            public static string LinkTable(this HtmlHelper helper, List<Method> items)
            {
                string result = String.Empty;

                foreach (Method m in items)
                {
                    result += String.Format(
                        "<label class=\"label2\">{0}</label>" +
                        System.Web.Mvc.Html.ActionLink(...) +
                        "<br />",
                        m.Category.Name,m.ID, m.Name);
                }

                return result;
            }


  }
}

Unfortunately Html.ActionLink is not recognized in this context whatever the namespace I have tried to declare.

As a generic question, I would like to know if it is possible to use any existing standard/custom Html helper method when designing a new custom helper.

Thanks.

2 Answers 2

55

Don't you have the helper already?

helper.ActionLink("text", "actionName");

Don't forget to include using System.Web.Mvc.Html namespace.

And yes, you could use the existing extension methods as long as you included the necessary namespaces.

0
4

FYI, for MVC 3, I found the ActionLink here:

System.Web.Mvc.Html.LinkExtensions.ActionLink(text, action, controller).ToHtmlString();
2
  • 1st parameter above must include reference to htmlhelper see below
    – LA Guy 88
    May 7, 2012 at 19:15
  • public static MvcHtmlString ActionLinkCustom(this System.Web.Mvc.HtmlHelper html, string menutext, string action, string controller) ....... string link = System.Web.Mvc.Html.LinkExtensions.ActionLink(html,menutext, action, controller).ToHtmlString();
    – LA Guy 88
    May 7, 2012 at 19:18

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.