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
    0

    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??

     

    Thanks

     

    Scott

     

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

    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;
    @xmlItem.Element("inBriefText").Value
    @xmlItem.Element("inBriefTitle").Value
    // not sure what to do with the media picker...
    }
  • Funka! 398 posts 661 karma points
    Aug 14, 2013 @ 05:22
    Funka!
    0

    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)
    {
        <tr>
            <td>@row.Cells[0].Value</td>
            <td>@row.Cells[1].Value</td>
            <td>@row.Cells[2].Value</td>
            <td>@row.Cells[3].Value</td>
        </tr>
    }
    
  • Michael Williams 1 post 21 karma points
    Jun 22, 2015 @ 17:47
    Michael Williams
    0

    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)
            {
                <div>@row.textString1</div>
                <div>@row.textString2</div>
               @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" />
                   }
               }
                <div>@row.textString1</div>
    
            }
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft