Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raw ActionLink linkText

I want to put a button as the text of an @ActionLink() but I can't because it HTML-escapes my string... I found the @Html.Raw() mechanism and have tried the @ActionLink().ToHtmlString() but can't figure out how to put it together...

I found an article that describes building an extension for a similar purpose but it's eeky to go to that much trouble... there must be an easy way?

like image 476
ekkis Avatar asked May 19 '11 18:05

ekkis


People also ask

What is HTML ActionLink ()?

ActionLink(HtmlHelper, String, String, String, String, String, String, Object, Object) Returns an anchor element (a element) for the specified link text, action, controller, protocol, host name, URL fragment, route values, and HTML attributes.

How do you pass an ActionLink model?

ActionLink is rendered as an HTML Anchor Tag (HyperLink) and hence it produces a GET request to the Controller's Action method which cannot be used to send Model data (object). Hence in order to pass (send) Model data (object) from View to Controller using @Html.

What is difference between HTML ActionLink and URL action?

Yes, there is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.


2 Answers

You could write a helper:

public static class HtmlExtensions
{
    public static IHtmlString MyActionLink(
        this HtmlHelper htmlHelper, 
        string linkText, 
        string action, 
        string controller,
        object routeValues,
        object htmlAttributes
    )
    {
        var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
        var anchor = new TagBuilder("a");
        anchor.InnerHtml = linkText;
        anchor.Attributes["href"] = urlHelper.Action(action, controller, routeValues);
        anchor.MergeAttributes(new RouteValueDictionary(htmlAttributes));
        return MvcHtmlString.Create(anchor.ToString());
    }
}

and then use this helper:

@Html.MyActionLink(
    "<span>Hello World</span>", 
    "foo", 
    "home",
    new { id = "123" },
    new { @class = "foo" }
)

which given default routes would produce:

<a class="foo" href="/home/foo/123"><span>Hello World</span></a>
like image 71
Darin Dimitrov Avatar answered Sep 27 '22 23:09

Darin Dimitrov


If you want to create a custom action link that uses T4MVC library, You can write the below code:

    public static System.Web.IHtmlString DtxActionLink(
        this System.Web.Mvc.HtmlHelper html, string linkText,
        System.Web.Mvc.ActionResult actionResult = null,
        object htmlAttributes = null)
    {
        System.Web.Mvc.IT4MVCActionResult oT4MVCActionResult =
            actionResult as System.Web.Mvc.IT4MVCActionResult;

        if (oT4MVCActionResult == null)
        {
            return (null);
        }

        System.Web.Mvc.UrlHelper oUrlHelper =
            new System.Web.Mvc.UrlHelper(html.ViewContext.RequestContext);

        System.Web.Mvc.TagBuilder oTagBuilder =
            new System.Web.Mvc.TagBuilder("a");

        oTagBuilder.InnerHtml = linkText;

        oTagBuilder.AddCssClass("btn btn-default");

        oTagBuilder.Attributes["href"] = oUrlHelper.Action
            (oT4MVCActionResult.Action,
            oT4MVCActionResult.Controller,
            oT4MVCActionResult.RouteValueDictionary);

        oTagBuilder.MergeAttributes
            (new System.Web.Routing.RouteValueDictionary(htmlAttributes));

        return (html.Raw(oTagBuilder.ToString()));
    }
like image 31
Dariush Tasdighi Avatar answered Sep 28 '22 01:09

Dariush Tasdighi