Copied to clipboard

Flag this post as spam?

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


  • Laurence Gillian 600 posts 1219 karma points
    Aug 27, 2013 @ 12:22
    Laurence Gillian
    2

    v6 API Documentation for MNTP

    Hello,

    I'm currently building some Razor Views and have found there's many ways to skin this cat.

    My preference is to use uQuery as it's consistent, but as a matter of principle, I'm building a new project using the new API's. Whilst looking at the documentation for the MNTP I've become a little confused, as (for me) it seems not to work.

    Umbraco Documentation - get Values from uComponents

    It seems that in the given examples, the API no longer exists. So I have updated the code to the following...

    @* look at the currentPage, does it contain any values for 'contentRelations'? *@
    @if (@CurrentPage.HasValue("contentRelations") && (@CurrentPage.contentRelations.ToString()) != "")
    {
       foreach (var x in CurrentPage.contentRelations) 
       {
       var n = @Umbraco.Content(@x.InnerText);
       // do something 
       }
    }
    
    1. Is this the right way to go about this?
    2. Could the check be better? (I know it's a MNTP, so can I use that to my advantage, rather than !="")
    3. If this is right, should I update the wiki?
    4. Should I write code add these items to the Model, rather than littering the view.

    Thanks! Laurence

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Aug 29, 2013 @ 10:08
    Jeavon Leopold
    0

    Hey Laurence,

    Have you considered a Property Editor Value Converter for MNTP? There is one included in this package

    Then you only need the following and x would be the IPublishedContent object

    @{
            var contentRelations = CurrentPage. contentRelations;
            foreach (var x in contentRelations)
            {       
                <p>@x.Name</p>           
            }    
    }
    

    Cheers,

    Jeavon

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Aug 29, 2013 @ 11:29
    Jeroen Breuer
    2

    If you use the strongly typed version you can also use extension methods. This is how I'm using MNTP:

    Extension method:

    /// <summary>
    /// Return the nodes selected with MNTP (xml only) as IPublishedContent.
    /// </summary>
    /// <param name="node"></param>
    /// <param name="propertyName"></param>
    /// <returns></returns>
    public static IEnumerable<IPublishedContent> GetMntpNodes(this IPublishedContent node, string propertyName)
    {
        var xml = node.GetPropertyValue<RawXmlString>(propertyName);
    
        if (xml != null)
        {
            var xmlData = xml.Value;
    
            if (!string.IsNullOrEmpty(xmlData))
            {
                var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
                return umbracoHelper.TypedContent(XElement.Parse(xmlData).Descendants("nodeId").Select(x => (x.Value))).Where(y => y != null);
            }
        }
    
        return Enumerable.Empty<IPublishedContent>();
    }

    Which you can use like this:

    @{
           
    var contentRelations =Model.Content.GetMntpNodes("contentRelations")
            foreach(var x in contentRelations)
           
    {      
               
    <p>@x.Name</p>          
           
    }    
    }

    Jeroen

  • Funka! 398 posts 661 karma points
    Aug 29, 2013 @ 20:40
    Funka!
    0

    I've had several frustrations over time dealing with MNTP's values as XML and now exclusively store them as CSV. Then I just get the value as a string and .Split(',') works great. I don't have to worry about the value's typing or structure changing over time, nor the unintuitive dealing with getting anything out of the XML which I used to do similar to how was shown above. Much easier now for me at least. Good luck to you!

Please Sign in or register to post replies

Write your reply to:

Draft