Search code examples
c#umbraco

What is the Strongly Typed version of CurrentPage.Id?


I am using Umbraco CMS and I am switching my code from dynamic to strongly typed.

I have been looking at replacing CurrentPage.Id with Model.Content.Id however visual studio is underlining the Id with a red line with the message:

Cannot convert method group 'id' to non-delegate type 'object'. Did you intend to invoke method?

Besides this it is working fine.

I tried using Model.Content.Id() which removes the red line however it throws the error:

CS1501: No overload for method 'Id' takes 0 arguments

Should I just use Model.Content.Id anyway? Can this visual studio 'error' be ignored? Is there a better way of doing this?


Solution

  • Model.Content.Id is correct, Model.Content is an IPublishedContent object with an Id property

    To make intellisense work you can add the following namespaces to the web.config located in the Views directory

    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="Umbraco.Web" />
        <add namespace="Umbraco.Core" />
        <add namespace="Umbraco.Core.Models" />
        <add namespace="Umbraco.Web.Mvc" />
        <add namespace="umbraco" />
        <add namespace="Examine" />
        <add namespace="Umbraco.Web.PublishedContentModels" />        
      </namespaces>
    </pages>