tags:

views:

3320

answers:

6

I can't find the Html.Image method in new MVC RC version. Please somebody give me example how to render simple Image in ASP .NET MVC RC sites.

A: 

What's wrong with pure HTML?

<img src="" alt="" />

You could use the ViewData collection or model to populate the src attribute:

<img src="<%= ViewData["ImageSource"] %>" alt="" />

<img src="<%= ViewData.Model.ImageSource %>" alt="" />

Edit:

Regarding using relative urls (i.e. using the "~" character) in the path, change the above code to use ResolveUrl. For example:

<img src="<%= ResolveUrl(ViewData.Model.ImageSource) %>" alt="" />
Kevin Pang
whats wrong? lack of verbosity, inability to easily find every place you used an image with a search, harder to switch out the implementation if for instance you decide to use amazon S3 and want to rewrite URLS
Simon_Weaver
I can see how it would be harder to search for your images, but I fail to see how this is any less verbose than an Html.Image tag that would have essentially the same information. I also don't see how it's harder to swap this implementation out (unless your complaint is the searching again).
Kevin Pang
A: 

Whats wrong? First: Can't use ~ character to build image path.

dario
Edited my previous answer to accommodate for using relative urls.
Kevin Pang
A: 

If you need to reference an image in, say, the ~/Content/images/ folder, it can easily be done with just plain HTML:

<img src="/Content/image/mypic.png" alt="a picture available anywhere" />

The trick is the "/" at the beginning of the src attribute, which will tell the browser to start browsing folders at the root of the site. As ASP.NET MVC by default does not parse route urls on files that do exist on the server, the file will be retrieved as usual. This will also work on the Index view of the Home controller regardless of whether you have the url http://www.example.com/, http://www.example.com/Home or http://www.example.com/Home/Index

Tomas Lycken
This won't work if you application is running in a virtual directory.
Garry Shutler
Garry's correct. I would recommend going with my ResolveUrl method to handle all cases, but if this works for you then that's fine too.
Kevin Pang
use MVC futures : http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24142
Simon_Weaver
+19  A: 

Need to write your own or use the Url.Content() method to set the source attribute. Or you can use mine. :-)

public static class HtmlHelperExtensions
{

    public static string Image( this HtmlHelper helper, 
                                string url,
                                string altText,
                                object htmlAttributes )
    {
        TagBuilder builder = new TagBuilder( "img" );
        builder.Attributes.Add( "src", url );
        builder.Attributes.Add( "alt", altText );
        builder.MergeAttributes( new RouteValueDictionary( htmlAttributes ) );
        return builder.ToString( TagRenderMode.SelfClosing );
    }
}

Usage:

<%= Html.Image( Url.Content( "~/Content/images/img.png" ),
               "alt text",
               new { id = "myImage", border = "0" } )
 %>

EDIT: Since I wrote this I've gone away from using it. I'll leave the answer here, though for posterity. While I think it, or the method from the Future assembly, is a reasonable choice, I find myself simply using the image tag with the url supplied by a UrlHelper. When I refactored my helpers into a standalone library, this one didn't make the cut. I'll re-evaluate if it makes it into the (non-futures) MVC library.

<img src="<%= Url.Content( "~/content/..." ) %>"
     alt="alt text"
     class="has-border" />
tvanfosson
why aren't you using Futures? http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24142
Simon_Weaver
1) I honestly didn't see it in futures when I wrote it and 2) I can depend on mine not being removed. If/when it makes it into the release, I'll change and use it.
tvanfosson
Perhaps I should get Resharper. Meanwhile, be sure to addusing System.Web.Mvc;using System.Web.Routing;
AlexanderN
TagBuilder builder = new TagBuilder( "image" );should beTagBuilder builder = new TagBuilder( "img" );
labilbe
Looks like the Futures library is MIA. Deprecated?
spoulson
@spoulson - the futures code still seems to be on CodePlex: http://aspnet.codeplex.com/SourceControl/changeset/view/23011
tvanfosson
A: 

Guys thanks! But where is my lovelly Html.Image method from previous release? :-)

dario
Actually, even if you do have an extension method, I'd recommend using plain html if you can. The better control of your markup you have, the closer the actual look will be to your intention ;)
Tomas Lycken
+2  A: 

Its in MVC futures - which they are updating with each release.

RC2 futures is here : http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24142

Don't feel guilty about using something in 'futures'. Its not going anywhere - and even if it ever did you have access to the source.

You need to be slightly cautious about some things in futures (in case they disappear) but you can be pretty sure Image helper isnt going anywhere.

Simon_Weaver
Generally things only "disappear" from the MVC Futures library as they move into the main System.Web.MVC namespace.
Zhaph - Ben Duguid