Domanda

I have a controller that loads an image file:

    //
    // GET: /MyController/Image/id
    public ActionResult Image(string id)
    {
        var dir = @"C:\Temp\Images";
        var path = Path.Combine(dir, id + ".png");
        return File(path, "image/png");
    }

In a separate view from action MyController/Page/id I have hastily hashed up some html to show the image:

<img src="../image/@Model.Name" />

And then also with a link:

<a href="../image/@Model.Name">
  <img src="../image/@Model.Name" />
</a>

It works, but I am looking for a better way using @Html so resharper can help with navigation and validation, for the two above. I've tried this:

@Html.RenderAction("Image", "MyController", new { id = Model.Name })

But this doesn't compile, it says Expression must return a value to render.

È stato utile?

Soluzione

I think you're just looking for Url.Action(), which returns an URL to an Action:

<img src="@Url.Action("Image", "ImageController", new { id = Model.ImageID })" />

Altri suggerimenti

You can use Url.Action() in the src attribute

 <img src='@Url.Action("Image", "ImageController", new { imageId = Model.Id })' alt='MyImage' />
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top