Copied to clipboard

Flag this post as spam?

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


  • Jacob 41 posts 90 karma points
    May 05, 2014 @ 17:38
    Jacob
    0

    Umbraco Library methods

    It would seem I'm encountering a rather unique issue with the Umbraco.library.StripHtml() method.

    I want to reference node content from the umbraco.library extension; however, I would like to keep the html configured from a richtext editor to display as html on the page.

     

    For instance, the following code returns my node content as plain text regardless of any formatting applied in the Umbraco richtext editor:

    @library.StripHtml(umbraco.NodeFactory.Node.GetCurrent().GetProperty("loginInfo").Value)

     

    I've also tried other methods like GetItem, but this encloses all my html in quotations so it renders as a string and not markup:

    @library.GetItem(umbraco.NodeFactory.Node.GetCurrent().Id,"loginInfo")

     

    I've tried other methods as well without any success.

     

    How can I reference Umbraco node content from umbraco.library without losing my richtext styling with methods such as StripHtml?  The idea is to retain certain styles applied via the richtext editor such as bold or hyperlinks and call them to a partial MVC view.

  • Damian Green 452 posts 1433 karma points
    May 11, 2014 @ 20:51
    Damian Green
    0

    Hi,

    Don't quite understand what you are trying to achieve. Styling of items in the rich text editor is done using HTML so using the strip html function is going to remove all that.

    A could of other things...

    @Html.Raw() will output html as html to the page.

    From my memory... If you want to remove certain html elements you could use something like the html agility pack: http://htmlagilitypack.codeplex.com I think you can transform html with that lib.

    Apologies if I'm miles off what you are after but can't quite understand your problem.

  • Jacob 41 posts 90 karma points
    May 12, 2014 @ 14:41
    Jacob
    0

    I knew that stripHtml would remove all styling from my umbraco content, which was the initial problem.  However, after thinking about your post, I went back and acheived the desired functionality utilizing the following syntax from my partial view:

     

    @Html.Raw(umbraco.NodeFactory.Node.GetCurrent().GetProperty("loginInfo").Value)

      

    The above code renders my Umbraco content WITH the styling!  It appears that I didn't need to use the Umbraco.library extension after all, but rather mix umbraco.NodeFactory with a standard Razor @Html.Raw method.  Issue resolved.

     

    Thanks for the help, Damian!

  • Damian Green 452 posts 1433 karma points
    May 12, 2014 @ 19:08
    Damian Green
    0

    What version are you using?

    In Model.Content.GetProperty("") is the better more recent way to access properties with MVC and Razor

    Model.Content is IPublishedContent

    Damian

  • Jacob 41 posts 90 karma points
    May 12, 2014 @ 20:24
    Jacob
    0

    I'm using Umbraco 7.  My scenario is I'm using a partial view that needs to reference Umbraco content and retain all richtext styling.  Umbraco first finds a base page through its own routing system, and from that page I'm calling an Html.Action to iterrate commands and render a subsequent partial view containing a viewmodel of information from my database.  Within the partial view, I cannot use the Model.Content.GetProperty("") method you mentioned, because it's now in the context of an MVC partial view and not IPublishedContent.

  • Damian Green 452 posts 1433 karma points
    May 12, 2014 @ 22:38
    Damian Green
    0

    Well where you call the partial view pass the Model to the partial like this:

    @Html.Partial("partialName", Model.Content)
    

    And then in your partial view inherit from

    @inherits UmbracoViewPage<IPublishedContent>
    

    Now you will just call

    @Model.GetProperty("alias").ToString()
    

    or you can also use a type converter like this:

    @(Model.GetProperty<string>("alias"))
    

    You have to put the brackets round the code because razor doesn't like the

    Hope that helps. Give it go.

    Damian

  • Damian Green 452 posts 1433 karma points
    May 12, 2014 @ 22:42
    Damian Green
    0

    Just noticed it sounds like you are not using a template page...

    Well a better method of getting a node is to use the Umbraco helper:

    var node = Umbraco.TypedContent(nodeId);
    

    That then returns an IPublishedContent of the node and you can do the above.

  • Jacob 41 posts 90 karma points
    May 13, 2014 @ 14:45
    Jacob
    0

    I like the last method you mentioned.  This seems like a very flexible approach.  My original issue is resolved, but I'll keep those suggestions in mind for future use!  Thanks for shedding some light on the topic.

Please Sign in or register to post replies

Write your reply to:

Draft