Copied to clipboard

Flag this post as spam?

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

  • Scott Robinson 54 posts 75 karma points
    Aug 11, 2013 @ 22:49
    Scott Robinson

    Does anyone have a working Razor snippet for Data Type Grid?

    This is driving me a little nuts. This would be a really useful extension it I could find any documentation that worked.

    I have a very simple requirement. USign Razor directly in a page template to pull data from a Data Type Grid with an alias of 'inBrief' and containing properties "inBriefText" (textstring), "inBriefTitle" (textstring) and  'inBriefIcon" (media picker).

    I've tried various option but no luck. The following snippet looks promising but I don't understand the reference to page property which I've replace below with ****. What would I put there?


    @using uComponents.Core
    @using umbraco.cms.businesslogic.datatype

    @foreach (uComponents.DataTypes.DataTypeGrid.Model.StoredValueRow item in Model.**********)
    @(((uComponents.DataTypes.DataTypeGrid.aliasOfDataTypeGrid_ModelBinder.StoredValueForModel)@item.Cells.FirstOrDefault(x => x.Alias == "aliasOfPropertyInGrid")).Value)


    Where 'aliasOfDataTypeGrid' is the name I have called to grid

    Where 'aliasOfPropertyInGrid' is a property in the grid like 'Title'

    Can anyone help??






  • Stephen 1 post 21 karma points
    Aug 12, 2013 @ 22:35

    This probably isn't ideal, but I just used the XML stored in the property. Try this:

    var inBriefXml = new DynamicXml((string)Model.getProperty("inBrief").Value);
    foreach (var item in inBriefXml)
    var xmlItem = item.BaseElement;
    // not sure what to do with the media picker...
  • Funka! 398 posts 661 karma points
    Aug 14, 2013 @ 05:22

    I struggled with this too somewhat recently, back in an umbraco 4.11.8 site with uComponents v5.3.2. I ended up having to resort to accessing my values by ordinal index since I could never get the access-by-name to work.

    Perhaps they've made this easier in the last five or so months since I last did this. And yes, this is somewhat brittle should you ever change the order of your datagrid fields or add/remove one, etc... But in any case, good luck to you!

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @foreach(var row in Model.myProp)
  • Michael Williams 1 post 21 karma points
    Jun 22, 2015 @ 17:47
    Michael Williams

    Although this is an old post, I struggled with this recently and discovered that documentation has not improved much with the very useful and powerful DataType Grid. However, I am posting this code snippet in hopes of helping the next person.

    Assuming that my DataType Grid has an alias of "myDTGrid" with the following datatype aliases contained within - textString1, textString2, mediaChoice:

    @inherits umbraco.MacroEngines.DynamicNodeContext
        if (Model.HasProperty("myDTGrid") && Model.HasValue("myDTGrid"))
            foreach (var row in Model.myDTGrid)
               @if (row.mediaChoice != null)
                   dynamic selectedMedia = @Library.MediaById(row.mediaChoice);
                   if (selectedMedia != null && selectedMedia.Id > 0 && selectedMedia.HasProperty("umbracoFile") && selectedMedia.HasValue("umbracoFile"))
                       <img src="@selectedMedia.umbracoFile" />
Please Sign in or register to post replies

Write your reply to: