1

Good day, I'm try to use action links with Url.Action inside @Html.Raw in my ASP.NET MVC3 project. Everything works fine without @Html.Raw, but with it images can't display on the page, I'm try to use Html.Encode inside Raw, but then it's show me the naked HTML on the page.

@Html.Raw(<a href="@Url.Action("ActionName", "Controller", new {id = 1})" target="_blank">
                <img src="@Url.Content("~/Content/Images/simpleImage.png")"/>
            </a>)

Any ideas why it's not render right code?Also when I hover over the place with image it pop up for me next code: sitename.com/Controller/@Url.Action(

I'm try to shielding the " it's not help

Edit

This action link is a part of query from database, which I display like next:

@Html.Raw(model.FieldWithHtmlCharactersInDatabase)
Rakstit
  • 57
  • 1
  • 13

4 Answers4

2

You need to pass string, try this

@Html.Raw("<a href='" + @Url.Action("ActionName", "Controller", new {id = 1})' " + target='_blank'>
                <img src='" + @Url.Content("~/Content/Images/simpleImage.png") " + "/>
            </a>")
Satpal
  • 132,252
  • 13
  • 159
  • 168
  • @Rakstit This works perfectly fine. Please post the exact code you are using rather than mention one thing but then comment about something else. – Tim B James Aug 26 '13 at 08:50
1

Try this instead where you are passing in the string variable and escaping the " correctly.

@Html.Raw("<a href=\"" + 
    Url.Action("ActionName", "Controller", new {id = 1}) 
    + "\" target=\"_blank\"><img src=\"" + 
    Url.Content("~/Content/Images/simpleImage.png") + "\"/></a>")

However I do not understand why you want this in @Html.Raw when this will give you the same output;

<a href="@Url.Action("ActionName", "Controller", new {id = 1})" target="_blank">
    <img src="@Url.Content("~/Content/Images/simpleImage.png")" />
</a>

Update

After you have updated your question, you are attempting to Execute Razor code from DB strings. I would suggest that you take a look at some of these questions on StackOverFlow;

ASP.net MVC: Execute Razor from DB String?

Pulling a View from a database rather than a file

Please note that your original question is very different from what you want.

Community
  • 1
  • 1
Tim B James
  • 20,084
  • 4
  • 73
  • 103
0

The following code:

<img src="@Url.Action("ActionName", "Controller", new {id = 1, width=100, height=100})" alt="" />

will yield this output:

<img src="Controller/ActionName/1?width=100&amp;height=100" alt="" />

You will get the same surrounding Action helper with Raw:

<img src="@Html.Raw(Url.Action("ActionName", "Controller", new {id = 1, width=100, height=100}))" alt="" />

So this must be a bug in Razor/MVC. One (tedious and ugly) workaround I found is this:

@Html.Raw("<img src=\"" + Url.Action("ActionName", "Controller", new {id = 1, width=100, height=100}) + "\" alt=\"\" />" )

Hopefully Microsoft will fix this.

0

If someone has the same problem in the future, for me it worked to remove the ~ infront of the image string!

  • I think you would do better to open your own topic by explaining your problem in more detail. You will have a better chance of getting help. – Enzo B. Mar 01 '19 at 13:14
  • @EnzoBLANCHON I dont have the Problem anymore it's just a fix for the problem above, if somebody has the same problem again. – schinkenwurfel Mar 05 '19 at 14:30