Copied to clipboard

Flag this post as spam?

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


  • David Beesley 5 posts 89 karma points
    Mar 29, 2016 @ 08:35
    David Beesley
    0

    Get ID or Unique Identifier for instance of document type in razor

    I'm sorry the title is awfully worded.

    When rendering out a document type in Razor using DTGE. Model.Content.Id always == 0. I'm trying to render out something that require unique html IDs - eg. a set of accordions - for JavaScript to use as hooks. Currently if I have two sets of accordions rendered out by the DTGE, the ID's in both sets are the same.

    Does each instance of a document type in each grid panel get a unique ID (or anything unique that I can reuse)? As far as I can tell, their IDs are all 0, and things like the URL etc are blank.

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Mar 29, 2016 @ 08:45
    Lee Kelleher
    101

    Hi David,

    Due to the nested nature of the DTGE data object (IPublishedContent) it is not a fully fledged "content node", as in it doesn't belong in the content tree, nor is it queryable from the content cache, (e.g. you can't do Umbraco.GetTypedContent(x) to return it).

    You'll need to find an alternative way to setting a unique ID in your partial-view/markup.

    Possible ideas are...

    • set an "ID" field in the DTGE doctype?
    • add a counter to your loop, then you'll have the current index/position of the item?
    • use an MD5 hash of the item's name? (e.g. Umbraco.CreateMd5Hash(Model.Content.Name)) but this requires the Name to be unique!

    I hope this helps?

    Cheers
    - Lee

  • David Beesley 5 posts 89 karma points
    Mar 29, 2016 @ 08:49
    David Beesley
    0

    Thanks Lee.

    I thought that may be the case. But was worth asking before I moved on.

    I have gone with a combination of DateTime.Now.Ticks (To identify the accordion set) and then a counter inside the foreach loop (to identify the accordion panel)

Please Sign in or register to post replies

Write your reply to:

Draft