0

I used the TinyMCE editor to post content to the database so I've got html codes in the database. When I tried outputting it on my MVC View, the details page displayed perfectly with all the html formatting but the readmore page is outputting some html raw characters. Below is the code for the details view which outputs my html properly:

<h3>
    @Html.Raw(Model.articleContent)
</h3>

And below is the code for the entire page where I have the read more.

<h3>
    @{
        var articleContent = @Html.Raw(item.articleContent);
        ViewBag.articleContent = articleContent.ToString().Substring(0,300);
    }

    @ViewBag.articleContent
</h3>

The output I get for the above is like the below content:

Some test article title

<p>Team Aquamarine &ndash; Ola Ehimigbai, Murtala Saleh<br />PT,CSS ,SD Office, GRC,HRT and SP, this is quite a merger and Roland Guobadia, Lukman Longe and Cajetan and Igbokwe would hope that theirs, is a team that would finish up in the medal table, the light blue color of team aquamarine is the f 

1
  • Can you point out precisely which line is outputting the incorrect text? I can't work it out cause you have 3 versions of articleContent... item.articleContent, ViewBag.articleContent and Model.articleContent and the code shows a <h3> and the output a <p> Commented Nov 3, 2015 at 8:25

1 Answer 1

2

You should use Html.Raw with ViewBag.articleContent because you want to display html which is in ViewBag.articleContent.

     ...
     @{
          ViewBag.articleContent = item.articleContent.Substring(0,300);
     }
     @Html.Raw(ViewBag.articleContent)
</h3>
...

Or you can even make it simpler:

...
<h3>@Html.Raw(item.articleContent.Substring(0,300))</h3>
...
Sign up to request clarification or add additional context in comments.

5 Comments

But ViewBag.articleContent already is the output of Html.Raw()
@Coulton Yes you are right but later there is ViewBag.articleContent = articleContent.ToString() and that make ViewBag.articleContent to be regular string.
What is the point in running Html.Raw() on it before then and surely articleContent is already a string so no need for ToString()?
I'd say it's better practice to set the ViewBag in the controller... even better practice would be to not use the view bag at all. You've got the variable in the view model... why use the viewbag?
@Coulton there is no need to run Html.Raw before but if you do this it return IHtmlString and then you need to use ToString. @Heberda Sure it is bad practice but here we see only part of code maybe there is some reason for this....

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.