Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • John Wilson 14 posts 107 karma points
    Oct 22, 2021 @ 12:49
    John Wilson
    1

    Unable to access RTE content in Umbraco 9

    In Umbraco v8, I would do the following to access RTE content:

    model.PageContent = content.Value<IHtmlString>("pageContent");
    

    In Umbraco v9, this is no longer working for me. I have tried the following:

    model.PageContent = content.Value<IHtmlContent>("pageContent");
    model.PageContent = content.Value<HtmlString>("pageContent");
    

    Either method returns null. Is there a new data type I should be using? I also used IHtmlEncodedString, but that returns the data escaped (so can't be placed in a HTML view).

    Any help would be very much appreciated. Thank you.

  • Frank Laumann 39 posts 303 karma points
    Oct 22, 2021 @ 13:19
    Frank Laumann
    0

    Hi John

    Looks like it might be IHtmlEncodedString

    enter image description here

  • John Wilson 14 posts 107 karma points
    Oct 22, 2021 @ 14:29
    John Wilson
    0

    It looks like retrieving the value as an IHtmlEncodedString at least gets me the content, and doesn't return null. Now my issue is issuing the output in my view.

    In the UmbracoViewPage.cs source code, there is a Write method that checks whether the input parameter is an IHtmlEncodedString, and if so, calls "WriteLiteral" on it. I'm wondering if I should be calling Write on each one of my model's properties (at least RTE/IHtmlEncodedString properties). This seems like an odd way to go about it, where in the previous versions I could just output the RTE content like:

    <div class="page-content">
        @Model.PageContent
    </div>
    
  • John Wilson 14 posts 107 karma points
    Oct 25, 2021 @ 19:55
    John Wilson
    0

    I am finding in some cases that I need to retrieve RTE content as a HtmlString, other times as a string which I then must place in a HtmlString, and then other times I can retrieve IHtmlEncodedString and use it with no issue.

    I am really not sure of the reason, but it seems like it may change based on whether the content is being used in a partial view, a view component, or directly in a template.

  • John Wilson 14 posts 107 karma points
    Oct 28, 2021 @ 15:35
    John Wilson
    101

    I manually map my published content to custom models, and the IHtmlEncodedString solution was not working for me whenever the content was output directly in a partial or ViewComponent.

    My solution was to retrieve the RTE content as a string, then place inside a new HtmlString, such as:

    string rteContent = content.Value<string>("rteContent");
    HtmlString rteValue = new HtmlString(rteContent ?? string.Empty);
    

    Using this solution, I can output the HtmlString anywhere without needing to call @Html.Raw around the property. I'm not sure if Umbraco 9 is supposed to be used this way, and would love to know if there is an easier way for me to retrieve RTE content on a custom model, and be able to output that property without any additional ceremony.

  • John Wilson 14 posts 107 karma points
    Oct 22, 2021 @ 13:52
    John Wilson
    0

    I used IHtmlEncodedString, but the code is output escaped (i.e. a paragraph tag is output as

    &lt;p&gt;
    
  • Frank Laumann 39 posts 303 karma points
    Oct 26, 2021 @ 09:40
    Frank Laumann
    1

    Hi John,

    I think you can fix that by inserting the rte content in @Html.Raw()

    Best regards Frank

  • andrew shearer 506 posts 653 karma points
    Feb 03, 2022 @ 04:10
    andrew shearer
    1

    any advances/updates on this topic? im just trying to understand why umbraco doesn't use HtmlString from dotnetcore

    thanks

  • John Wilson 14 posts 107 karma points
    Feb 03, 2022 @ 13:16
    John Wilson
    0

    I am still retrieving a string, and then placing in a HtmlString. I haven't found a better way to do this.

Please Sign in or register to post replies

Write your reply to:

Draft