views:

11020

answers:

8

In ASP.NET MVC is there an equivalent of the Html.ActionLink helper for Img tags?

I have a controller action that outputs a dynamically generated JPEG and I wanted to use the same Lambda expressions to link to it as I do HREFs using ActionLink.

Alternatively, a helper that just gives the URL to a route (again specified using Lambdas) would also be acceptable.

EDIT: I had originally specified that I was using Preview 5, however I see that a Beta has been released. So all-in-all the version number was an unneeded piece of info as I may be upgrading soon :-)

+7  A: 

Url.Action() will get you the bare URL for most overloads of Html.ActionLink, but I think that the URL-from-lambda functionality is only available through Html.ActionLink so far. Hopefully they'll add a similar overload to Url.Action at some point.

stevemegson
I was afraid of that.
Jason Whitehorn
The parse-the-lambda functionality is in a nice convenient method in MvcToolkit, it's just internal. You could add your own extension method that calls it through reflection (or recompile MvcToolkit with it made public), assuming that an official one will replace it shortly.
stevemegson
If you make a nice extensionmethod out of this (something like: Html.ActionImage()), it shouldn't clutter your views :)
Erik van Brakel
+4  A: 

In ASP.NET MVC Beta, you can use the Html.BuildUrlFromExpression method in the Futures assembly (which is not included in the default ASP.NET MVC install, but is available from CodePlex) to create a link around an image--or any HTML--using the lambda-style ActionLink syntax, like this:

<a href="<%=Html.BuildUrlFromExpression<MyController>(c => c.MyAction())%>">
     <%=Html.Image("~/Content/MyImage.gif")%>
</a>

To keep your image links borderless, you'll need to add a CSS rule like this:

img
{
     border: none;
}
adamjcooper
+6  A: 

I used a workaround to place a marker instead of text for ActionLink and then replace it with my image code. Something like this:

<%= Html.ActionLink("__IMAGE_PLACEHOLDER__", "Products").Replace("__IMAGE_PLACEHOLDER__", "<img src=\"" + myImgUrl + "\" />")%>

Not the most elegant solution but it works.

Alan Mendelevich
This is what I'm doing, except I have an extension method for building the img tag. It'd be great to wrap this up in an extension method itself, and I just might do that.
Chris Charabaruk
+20  A: 

This question is older, and I just started recently with ASP.NET MVC when the RC was already out, but for those who find this question later like me this might be interesting:

At least in the RC you can use Url.Action() also with anonymous types, the result looks much nicer than the suggestions above, I guess:

<a href="<%= Url.RouteUrl("MyRoute", new { param1 = "bla", param2 = 5 }) %>">
   put in <span>whatever</span> you want, also <img src="a.gif" alt="images" />.
</a>

There are many other overloads for RouteUrl as well, of course.

markus
+1  A: 

You can use this control.It behaves like ActionLink.

http://agilefutures.com/index.php/2009/06/actionimage-aspnet-mvc

+1  A: 

Is Url.Content() what you're looking for?

Give it something like Url.Content("~/path/to/something.jpg") it will turn it into the appropriate path based on the application root.

-Josh

+12  A: 

You can use the URL.Action method

<a href="<%= Url.Action("Create")  %>"><img src="../../Content/Images/add_48.png" /></a>
Robert MacLean
This worked great! Just passed in the action and the controller and it worked like a charm! Thank you
MyWhirledView
+1  A: 

It's pretty simple to achieve in MVC 2. I have created my own very simple extension method to support Lambda expressions for the Url.Action helper. It requires that you reference MVC 2 Futures.
Here's the code:

using System;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Routing;

using ExpressionHelperInternal=Microsoft.Web.Mvc.Internal.ExpressionHelper;

namespace Bnv.Bssi.Web.Infrastructure.Helpers
{
    public static class UrlExtensions
    {
        public static string Action<TController>(this UrlHelper helper, Expression<Action<TController>> action) where TController : Controller
        {
            RouteValueDictionary routeValuesFromExpression = ExpressionHelperInternal.GetRouteValuesFromExpression<TController>(action);

            return helper.Action(routeValuesFromExpression["action"].ToString(), routeValuesFromExpression);
        }
    }
}

This is how you use it:

<img src="<%= Url.Action<YourController>(c => c.YourActionMethod(param1, param2)); %>" />
brainnovative