Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ASP.NET MVC Html Helpers from a custom helper?

Tags:

asp.net-mvc

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.

like image 939
stpe Avatar asked May 18 '09 07:05

stpe


People also ask

What are HTML helpers in MVC ?-?

In MVC, HTML Helper can be considered as a method that returns you a string. This string can describe the specific type of detail of your requirement. Example: We can utilize the HTML Helpers to perform standard HTML tags, for example HTML<input>, and any <img> tags.

Should I use tag helpers or HTML helpers?

Tag Helpers are attached to HTML elements inside your Razor views and can help you write markup that is both cleaner and easier to read than the traditional HTML Helpers. HTML Helpers, on the other hand, are invoked as methods that are mixed with HTML inside your Razor views.

Where do we use HTML helpers?

An HTML Helper is just a method that returns a HTML string. The string can represent any type of content that you want. For example, you can use HTML Helpers to render standard HTML tags like HTML <input>, <button> and <img> tags etc.


1 Answers

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.

like image 157
Matt Kocaj Avatar answered Oct 23 '22 20:10

Matt Kocaj