0

Project is based on as.net mvc and i am using Razor as the view engine.

I am using LINQ2Sql classes to extract data from the database. In a one particular column there is content with html formatting.

While extracting this data using LINQ and then putting it in a viewbag and then using it in a display view. The formatting got lost because of the fact the < brackets are converted to &lt and /&gt.

How to avoid this?

2
  • 1
    How are you rendering your display-view? Commented Aug 6, 2014 at 9:23
  • i have a repository class which will fetch the data. It will get called in my controller and assigned to a var type variable. Then this var variable is assigned to a viewbag property and then controller redirect to a view where i fetch the data from this viewbag using foreach. Commented Aug 6, 2014 at 9:36

2 Answers 2

1

You could use the following method:

System.Web.HttpUtility.HtmlDecode("your string")

For further documentation about this method, please have a look here .

The above method is helpful provided that your html is stored as encoded html. Otherwise it will no be helpful. As Mathew pointed out in his comment, then you should use the following method:

@Html.Raw("your string")
Sign up to request clarification or add additional context in comments.

3 Comments

I think that's a classic issue. That's why I suggested this method.
If his database already contains non-encoded html this is not of any help. He'll need @Html.Raw(string) to convert it to a MvcHtmlString and bypass the auto encoding. You might want to include that in your answer.
I am glad that I could help!
0

Render HTML directly by either wrapping it in an MvcHtmlString instance, or rendering it directly with <%: myHtmlString %>.

1 Comment

i think this one is applicable only for aspx engine

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.