Search code examples
umbracoumbraco6

Umbraco How to read content items using ASP.NET MVC Partial View and Razor


I have some special document type in Umbraco and in content tree I have one item that contains sub-items with this document type. I don't want to use VS 2012. and I want to create some partial view for read this items. and create some html markup. I created, partial view via umbraco UI,

@inherits Umbraco.Web.Mvc.UmbracoViewPage<dynamic> 

How to read all item and subitems in my view just with Umbraco API ?

Thanks.


Solution

  • You should inherit from Umbraco.Web.Mvc.UmbracoTemplatePage in your partial view:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    

    Then pass into your partial the current model:

    @Html.Partial("MyPartialName", Model.Content)
    

    Then in your partial you can just the API to get the children or whatever your query is:

    @foreach (var node in Model.Children().Where(x => x.DocumentTypeAlias == "YourDocTypeAlias")
    {
       <p>@node.Name</p>
    }