Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Claushingebjerg 936 posts 2571 karma points
    Apr 24, 2017 @ 12:01
    Claushingebjerg
    1

    Models builder and partials

    Imtrying to transition from the dynamic @CurrentPage to Strongly typed and Models builder in preperation for V8. I have a question about partials though. Seeing as the models builder is hooked to the document type, how do you go about using the same partial on different doc types? This would often happen, if you use doc type compositions.

    For now i have it working by using the same inherits and using on both the template and partial, eg:

    @inherits UmbracoViewPage<ContentModels.Home>
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    

    But what should i do, if i want to use the partial on the doctype "Textpage" as well as the the "Home" doctype?

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Apr 24, 2017 @ 13:05
    Alex Skrypnyk
    0

    Hi Claushingebjerg

    Do not use @inherits UmbracoViewPage

    You have to write some logic for getting needed properties.

    If aliases of properties are the same between all docTypes, you can use "Document Type Compositions", move all properties to composition and then ModelsBuilder will create an interface for you, with your properties:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage

    Hope it will make sense for you.

    Thanks,

    Alex

  • Claushingebjerg 936 posts 2571 karma points
    Apr 24, 2017 @ 13:33
    Claushingebjerg
    0

    Hi Alex. Not really :), and it throws an error in the partial if i change @inherits UmbracoViewPage<ContentModels.Home> to @inherits Umbraco.Web.Mvc.UmbracoViewPage

     'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'Opacity' and no extension method 'Opacity' accepting a first argument of type 'Umbraco.Core.Models.IPublishedContent' could be found 
    

    "Opacity" is a doc type parameter on a doctype composition.

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Apr 24, 2017 @ 13:38
    Alex Skrypnyk
    0

    And what is the name of your composition?

    Can you share the code of all partial?

    It should be:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<ICompositionName>
    
  • Claushingebjerg 936 posts 2571 karma points
    Apr 24, 2017 @ 13:46
    Claushingebjerg
    0

    Sure. I've changed it as i understand you. The composition is called "Sidetop"

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<Sidetop>
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    
    
        <section class="imagebg height-50"  data-overlay="@Model.Opacity">
            <div class="background-image-holder background--bottom">
                <img alt="image" src="@Umbraco.Media(@Model.Topbillede).Url" />
            </div>
            <div class="container pos-vertical-center">
                <div class="row">
                    <div class="col-sm-12 text-center">
                        <h1>@Model.Overskrift</h1>
                <p class="lead">@Model.Manchet</p>
                    </div>
                </div>
            </div>
        </section>
    

    Now throws the following error

    Cannot bind source content type Umbraco.Web.PublishedContentModels.Kunstnere to model type Umbraco.Web.PublishedContentModels.Sidetop.
    
  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Apr 24, 2017 @ 13:47
    Alex Skrypnyk
    102

    Try this one:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage

    Composition name with "I" prefix.

  • Claushingebjerg 936 posts 2571 karma points
    Apr 24, 2017 @ 13:59
    Claushingebjerg
    0

    Yup, that did the trick

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<ISidetop>
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    

    Do you have any documentation on how and why it should be like this or do .net devs just know this "magic"?

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Apr 24, 2017 @ 14:01
    Alex Skrypnyk
    3

    Look please this article - http://24days.in/umbraco-cms/2016/getting-started-with-modelsbuilder/

    The models builder respects the content type's inheritance, and content type's compositions are represented by interfaces.

    To see how the models are generated then you can go into ~/AppData/Models/models.generated.cs and /AppData/Models/all.generated.cs

  • Claushingebjerg 936 posts 2571 karma points
    Apr 24, 2017 @ 14:42
    Claushingebjerg
    0

    Final question :)

    How do i handle it, if i need to use the same template with 2 different doc types?

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Apr 24, 2017 @ 16:02
    Alex Skrypnyk
    0

    I see these ways:

    1. GetPropertyValue method and do not use Strongly typed models at all

    2. Composition with all used properties or parent document type

    3. A lot of if statements with checking is there this property now or no - in other words, make templates general for document types

    Hope it makes sense for you.

    Thanks,

    Alex

  • Claushingebjerg 936 posts 2571 karma points
    Apr 25, 2017 @ 06:47
    Claushingebjerg
    0

    In this case number 2 or 3 makes sense as the only difference between the two doc types are internally used fields from a composition.

    The doctype aliases are "Kunstner" and "importeretKunstner"

    @inherits UmbracoViewPage<ContentModels.Kunstner>
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    

    Only handles the "Kunstner" Doctype...

    @inherits UmbracoViewPage<ContentModels.Kunstner>
    @inherits UmbracoViewPage<ContentModels.ImporteretKunstner>
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    

    Seems to work, but is it the right way to do it?

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    May 12, 2017 @ 21:27
    Alex Skrypnyk
    0

    Hi Claushingebjerg

    As I know there is no sense to use 2 @inherits It uses only first one

    This is the right way to handle compositions:

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

    Hope it make sense

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft