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)
4
  • 1
    if Everything works fine without @Html.Raw, then why are you using it? Commented Aug 26, 2013 at 8:13
  • because I'm add this info to database, and need to display it on the page Commented Aug 26, 2013 at 8:27
  • Where does this @Html.Display relate to @Html.Raw? Commented Aug 26, 2013 at 8:59
  • You are using very bad design of storing markup in the database, I will strongly recommend you to refactor your strategy of storing image and anchor link. Commented Aug 26, 2013 at 9:06

4 Answers 4

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>")
Sign up to request clarification or add additional context in comments.

1 Comment

@Rakstit This works perfectly fine. Please post the exact code you are using rather than mention one thing but then comment about something else.
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.

3 Comments

Because it's a part of string from my database which I display on the page inside @Html.Raw(model.SomeValue)
Where is this mentioned in your question?
@Rakstit This works perfectly fine. Please post the exact code you are using rather than mention one thing but then comment about something else.
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.

Comments

0

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

2 Comments

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.
@EnzoBLANCHON I dont have the Problem anymore it's just a fix for the problem above, if somebody has the same problem again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.