Search code examples
umbracoumbraco7

How to access newly created umbraco document type in Visual Studio razor view


I am an umbraco newbie. I created a new document type with a simple string, colour property, like below.

umbraco screenshot

However, when I created a new VisualStudio razor view for a template for this document type, it obviously doesn't know what MyFavouriteColour content model is, so I am unable to use it.

Using the Umbraco UI however, things work fine but I want to work with visual studio only, instead of using umbraco UI. Any help greatly appreciated, thanks.

@using Umbraco.Core;

@using Umbraco.Core.Models;
@using Umbraco.Core.Services;
@using ContentModels = Umbraco.Web.PublishedContentModels;
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.MyFavouriteColour>
@{
    ViewBag.Title = "title";
    //Layout = "_Layout";
}

<h2>title</h2>
@Umbraco.Field("favoriteColor")

Solution

  • When you create a new Document Type, it asks you if you want to create a Template as well. When the Template is added, it creates a predefined razor you can add in your VS solution. You can also add the razor file manually.

    Your razor file should look like this:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        Layout = "_Layout";
    }
    
    <h1>@CurrentPage.FavoriteColor</h1>
    

    Where @CurrentPage.FavoriteColor is a custom field you defined in your Document Type.