Copied to clipboard

Flag this post as spam?

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

  • Ranjit J. Vaity 52 posts 95 karma points
    Jun 12, 2018 @ 13:12
    Ranjit J. Vaity

    Multiple DocType using One Controller

    Hello Community,

    I have been working with Umbraco since a while and haven't faced such case. I hope I might get some reply / suggestion / advise from the community.

    I am working on a News/Media Doctypes where project requirement say about few properties that are mandatory on some types of page and some might not be madatory.

    Ex: News pages with Sports type should have reporter field mandatory but for Politics type reporter field as non-mandatory.

    So I ended up creating 2 doc types to match the requirements. I also think that we can use same template(view) for both doctype.

    I am trying to find a way or a approch to do the same in case of controllers coz I have to do extra content processing before rendered to UI.

    My question: Is there a way I can route all calls from SportsNewsDocType and PoliticsNewsDocType to one Controller called "NewsDocType"?

    Please suggest..

    Thank you in advance!


    Ranjit J. Vaity

  • Søren Gregersen 103 posts 532 karma points c-trib
    Jun 12, 2018 @ 13:54
    Søren Gregersen


    You can override the default controller, with your own, and then add the logic you need - have a look at this

    If you've created your doctypes using compositions, and are using modelsbuilder, you are able to just check if a document implements any of the compositions, by just checking of the document "is ISportsNews".

    The compositions could be: StandardArticle (normal doctype, with template) SportsArticle (no template) PoliticsArticle (no template)

    The models build from the above will StandardArticle (class), ISportsArticle (interface) and IPoliticsArticle (interface)

    In your controller you can then do (not tested):

    if(content is StandardArticle article) {
      if(article is IPoliticsArticle as politics) return view("polistics", politics)
      if(article is ISportsArticle as sports) return view("sports", sports)
      return view("article", article)

    (maybe some c# 7+ features in the above code)

  • Ranjit J. Vaity 52 posts 95 karma points
    Jun 12, 2018 @ 14:48
    Ranjit J. Vaity

    Hello Søren,

    I thank you for your reply and was a quick one. :)

    I am not using modelbuilder and we have created our own model (VM and Dtos) to move data from custom service classes to Controllers and later to views.

    So what would be the other aproach I can go with coz we are almost in mid-way project and cannot switch to modelbuilder.

    Also please advise how can I implement Interfaces where I can find out which type of Article is called from in my only controller like StandardArticleController

    Thank you! :)

    Regards, Ranjit J. Vaity

  • Søren Gregersen 103 posts 532 karma points c-trib
    Jun 12, 2018 @ 15:58
    Søren Gregersen

    The structure of the content/doctypes would be the same.

    I guess you are doing some mapping yourself in your controllers / services. They should just be able to handle the fields. If you don't have modelsbuilder, you will not be able to use the benefits it gives

  • Dan Diplo 1316 posts 4880 karma points MVP 2x c-trib
    Jun 12, 2018 @ 14:42
    Dan Diplo

    Another option is to abstract the "extra content processing" to a shared method. So you can still have two controllers, with different models, but the processing is performed by a new method.

    You'd need to either make your models inherit from a base class or implement an interface and then you could pass either of them to this processing method before you returned them to the same view.

    This would also ensure your controllers don't get too bloated, as generally a controller shouldn't perform much logic.

  • Ranjit J. Vaity 52 posts 95 karma points
    Jul 04, 2018 @ 07:27
    Ranjit J. Vaity


    Thank you very much for replies and suggestions. As the business requirements were very specific about the properties and their mandatory behaviors we had to go with creating separate document-type with multiple controllers but our templates remained the same. As advised all the processing logic is moved to single place instead of bloating controllers.

    I thank you all again! :)

    Regards, Ranjit J. Vaity

Please Sign in or register to post replies

Write your reply to: