Search code examples
razorumbracoumbraco7

Get Node Id of Umbraco content


I am creating a select element list from Umbraco content

I can navigate to the nodes I want to get the data from (benCatItem below) - when at benCatItem, how do I get the Id of the element? When I debug, I can see it under Content as an int of Id - but cannot work out how to set a variable as the value

@{ foreach (var benCat in Model.Content.Children (c=>c.DocumentTypeAlias.Equals("benefitCategory", StringComparison.CurrentCultureIgnoreCase)))
                {
                    string catLabel = benCat.GetPropertyValue<string>("benefitCategoryLabel");
                    <optgroup label="@catLabel">
                        @{ foreach (var benCatItem in benCat.Children)
                            {

                                string catItemLabel = benCatItem.GetPropertyValue<String>("BenefitCategoryItemLabel");
                                string catItemLinkURL = benCatItem.GetPropertyValue<String>("BenefitCategoryItemLinkURL");
                                string catItemLinkText = benCatItem.GetPropertyValue<String>("BenefitCategoryItemLinkText");

I need the Id to populate the value of the select option for reference purposes


Solution

  • Your benCatItem variable is an instance of IPublishedContent so you should be able to access the Id property as follows:

    var catItemId = benCatItem.Id;
    

    For a complete list of the default Umbraco properties please see the official docs: https://our.umbraco.org/documentation/reference/querying/ipublishedcontent/Properties