0

I need to parse some HTML from the server side. So I've used

@Html.Raw(MyFunction("key")) 

MyFunction returns me the below value

<!--some HTML --> <li> Please click the link here to see the <a href='@(MySiteSettings.DocumentURL + "Documents/MyPDFFile.pdf")' target="_blank">Changes to Your Screens</a>. </li> <!--some other HTML --> 

I am expecting the anchor tag to be rendered like,

<a href='http://mydocumenturl/Documents/MyPDFFile.pdf' target="_blank">Changes to Your Screens</a> 

but it is rendered instead as below.

<a href='http://mycurrenturl/@(MySiteSettings.DocumentURL + "Documents/MyPDFFile.pdf")' target="_blank">Changes to Your Screens</a> 

I have already tried

@@(MySiteSettings... 

,

@:(MySiteSettings... 

and

@:@@(MySiteSettings...

For escaping '@' in Html.Raw, but no luck.

3
  • 1
    Razor sees that as text and does not compile it. It doesn't work that way.. I don't think you can do anything other that generating the full link inside your function. Commented Sep 25, 2014 at 8:27
  • why dont you use ViewBag.Link = "<a href='" + HttpContext.Current.Request.RequestContext.RouteData.Values["Controller"] + "/Documents/MyPDFFile.pdf'" + ">Changes to Your Screens</a>"; in view n use @Html.Raw(ViewBag.Link) instead Commented Sep 25, 2014 at 9:31
  • @AfterGlow, because I am fetching localized text from the DB for my content, depending on the language selected by the user. Commented Sep 25, 2014 at 9:35

3 Answers 3

1

@Html.Raw is doing what it's designed to do: This method wraps HTML markup using the IHtmlString class, which renders unencoded HTML. I don't have visual studio now, so please use this as a sample and update accordingly

Based your code, i would recommend you to use PartialViews

Somewhere in your current view:

@Html.RenderAction("PartialList"); 

PartialList ActionResult

[OutputCache(Duration = 60, VaryByParam = "None")] public ActionResult PartialList() { ... //form up your dynamic <a> here... return PartialView(<pass in your string here>); ... } 

PartialView

@Html.Raw(Model) <li> Please click the link here to see the <a href='@Model)' target="_blank">Changes to Your Screens</a>. </li> 

Then you might ask, why do you have to go through all this trouble? Take a look at this Not mentioned in that answer is the OutputCache which you can leverage on easily.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for the partial view suggestion. But I came up with something more trivial.
1

Define your function like this:

@helper MyFunction(string key) { // do your code here, also with @expressions <li> Please click the link here to see the <a href='@(MySiteSettings.DocumentURL + "Documents/MyPDFFile.pdf")' target="_blank">Changes to Your Screens</a>. </li> } 

2 Comments

+ 1 for the approach. I did not knew one could write functions inside cshtml. But it does not solve my problem as I am fetching everything from the database including the problematic link, for localization purpose.
then better to use partials.
0

I came up with this solution.

@Html.Raw(string.Format(MyFunction("key"), MySiteSettings.DocumentURL + "Documents/MyPDFFile.pdf")) 

And I replaced the content in my DB as below for the above to work.

<!--some HTML --> <li> Please click the link here to see the <a href='{0}' target="_blank">Changes to Your Screens</a>. </li> <!--some other HTML --> 

2 Comments

just be wary on how often this raw is appearing in your view, because everytime it's being displayed, it queries your db
@Lee Gary, we are using Cache for storing the db values and we have implemented SQL Cache dependency. So it won't query the db every time. Thank you for the raising the point though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.