Retrieve Widget identifier (GUID) in viewcomponent context (MVC)

Bob Haring asked on January 4, 2023 08:53

Currently I'm working on something where I need to retrieve the identifier property that is present that is present in the Configuration property of the IPageBuilderDataContext object.

In that property you have the properties from everything on a page but I need to know the current widgets identifier, since I want to add the identifier as an attribute in the HTML of my code.

So my question is how do I retrieve this? Thanks.

Correct Answer

Elmar Höfinghoff answered on January 6, 2023 09:43

In case you just need an individual id: Use a new Guid variable in your widget viewmodel and set this as id attribute in addition to a common name, e.g. <div id="accordion-group-@model.Guid"> Handle every referenve to this element in the same way using the same Guid. It doesn't have to be the same identifier Kentico uses to render it.

0 votesVote for this answer Unmark Correct answer

Recent Answers


Bob Haring answered on January 4, 2023 10:04

For example, I would like the identifier property of this:

"widgets": [
              {
                "identifier": "e0750aa5-171e-4b0c-9e57-81c7e968e9cf",
                "type": "CUSTOM.ExampleWidget",
                "variants": [
                  {
                    "identifier": "d8f5f3ac-f784-4267-99f0-053433bd0320",
                    "properties": {                   
                    }
                  }
                ]
              },
              ]
0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on January 4, 2023 13:46

Do you specifically need THAT identifier or just a new identifier?

0 votesVote for this answer Mark as a Correct answer

Bob Haring answered on January 5, 2023 06:50

It doesn't have to be exactly that one, just an identifier that's unique for each instance of a widget placed on a page.

0 votesVote for this answer Mark as a Correct answer

Bob Haring answered on January 10, 2023 04:58

Alright thanks for that advice, I'll do just that create my own ID property and work with that. I just hoped that the ID that is used internally for a widget would have been exposed so that there was no need for a duplicate ID property.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on January 11, 2023 15:29 (last edited on January 11, 2023 15:29)

In your view you can use this:

@{ var targetID = @Html.GenerateUniqueId("targID"); }

This will generate a unique ID based on the text targID. It works pretty slick for us and doesn't require any code changes to your ViewComponent or Model.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.