Copied to clipboard

Flag this post as spam?

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


  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Nov 30, 2012 @ 00:56
    Jeroen Breuer
    0

    MNTP in new Razor (MVC)

    Hello,

    I'm trying to find the best way to use MNTP in the new Razor views in MVC (4.11). For the old Razor code I used the Razor DataType Model, but this doesn't work in 4.11 yet. 

    Here is my current code:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using System.Xml.Linq;
    
    @{
        Layout = "Master.cshtml";
    }
    
    <p>Current page: @Model.Content.Name</p>
    
    <br />
    <p>Selected pages dynamic:</p>
    @foreach (dynamic d in CurrentPage.selectPages)
    {
        dynamic node = Umbraco.Content(d.InnerText);
        <p><a href="@Umbraco.NiceUrl(node.Id)">@node.Name</a></p>
    }
    
    <br />
    <p>Selected pages strongly typed:</p>
    @{
        var selectPages = Model.Content.GetProperty("selectPages").Value.ToString();
        if(!string.IsNullOrEmpty(selectPages))
        {
            foreach (var d in XElement.Parse(selectPages).Descendants("nodeId"))
            {
                var node = Umbraco.TypedContent(d.Value);
                <p><a href="@node.NiceUrl()">@node.Name</a></p>
            }
        }
    }

    Is there a better way to do this? Eventually it should be possible to use the IPropertyEditorValueConverter, but looking at the best alternative for now.

    Jeroen

  • Floris Robbemont 57 posts 89 karma points c-trib
    Nov 30, 2012 @ 01:29
    Floris Robbemont
    1

    I currently use:

     

    public static IEnumerable<Node> GetMultiNodeTreePickerNodes(this Node node, string propertyName)
    {
                var xmlData = node.GetProperty<string>(propertyName);
                return uQuery.GetNodesByXml(xmlData);
    }

     

    This uses the 'old' NodeFactory, but for the purpose of using 'proven techniques' this works fine :)
    Based on your code sample the Node type should give you all the properties you need. 

    If you want to use the IPublishedContent types, you could write the following snippet. Don't know which method of getting the actual nodes from the store is faster. I tend to think the second one is (or should be).

    public static IEnumerable<IPublishedContent> GetMultiNodeTreePickerNodes(this IPublishedContent node, string propertyName)
    {
    var xmlData = node.GetPropertyValue<string>(propertyName);
    var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);

    // Two methods of gettings the data from the Umbraco Store. Don't know which one is faster (I think the second one)
    // Query all and select single pages from store
    return XElement.Parse(xmlData).Descendants("nodeId").Select(x => umbracoHelper.TypedContent(x.Value));

    // Query all into collection and select in one method from store
    return umbracoHelper.TypedContent(XElement.Parse(xmlData).Descendants("nodeId").Select(x => (x.Value)));
    }
  • Troels Larsen 75 posts 280 karma points
    Dec 06, 2012 @ 13:44
    Troels Larsen
    3

    I dropped the XML and switch the mntp to use csv. that way i can juuse some of the lovely overloads of Umbraco.TypedContent that takes a collection of ids and return an collection of content

    IPublishedContent navigationSettings = Umbraco.TypedContent(SomeServiceNodeID eg 1050);
    var ServiceNavigationItems = Umbraco.TypedContent(navigationSettings.GetPropertyValue("serviceNavigation").ToString().Split(','));

    or if the mtnp is on the current model 

    var items = Umbraco.TypedContent(Model.Content.GetPropertyValue("topNavigation").ToString().Split(','));

    //Troels 

Please Sign in or register to post replies

Write your reply to:

Draft