Copied to clipboard

Flag this post as spam?

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


  • Harry Spyrou 212 posts 604 karma points
    Jul 27, 2018 @ 17:44
    Harry Spyrou
    0

    Old project without custom models merged with new project with custom models

    Hello Umbracians,

    I have a question. I have a client that wants to extend a very big ass older project that we had build with PureLive models and properties being like:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @Model.GetPropertyValue<string>("bla")
    

    I had used nested content and so most of the website is modularized and the view is being created by a loop that loops through the partials.

    @foreach (var item in Model.GetPropertyValue<IEnumerable<IPublishedContent>>("collection"))
    {
        var partialViewName = "_" + item.DocumentTypeAlias;
        if (partialViewName == "_contactForm")
        {
            Html.RenderPartial("_ContactForm", new CustomerContactModel(Model, item));
        }
        else
        {
            @Html.Partial(partialViewName, item);
        }
    }
    

    Now we are about to create a version two of the website and they want to keep all the old modules. Which means, abandoning the last project and starting a new one, won't be doable.

    I want to move the PureLive mode into my custom models and have:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<MyModel>
    @Model.PropertyInTheModel
    

    But the PureLive past is holding me back and the billable hours are not enough for me to go back and restart and recreate every view and partial view through custom models etc. I will have time to work only on the new views.

    The question is, is there a way to have both working? Old views will stay with the PureLive mode and my custom models will also work.

    Hope I'm making sense and thanks to everyone that responds.

    Regards

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Jul 30, 2018 @ 07:31
    Simon Dingley
    0

    I'm not sure I 100% understand the issue here. Have you even attempted this as the following should continue to work regardless of your implementation with custom models in any new views?

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @Model.GetPropertyValue<string>("bla")
    

    The above is not even using "PureLive" models and simply accessing the properties by alias on the IPublishedContent model for the current page which is essentially what ModelsBuilder models are doing under the hood.

  • Harry Spyrou 212 posts 604 karma points
    Jul 30, 2018 @ 08:40
    Harry Spyrou
    0

    Hello Simon,

    You're right it was my mistake it's not pure live. There's no issue per se, I'd like to just have my custom models in the new partials I'm going to develop.

    I think where the problem will rely is when trying to use the loop that renders the page. Half the partials will be cusom models and half of them as I showed you before.

    I'll have to render each partial with its model then, using @Html.RenderPartial and passing the model. Is there a more compact way I can do that? Adding tons of 'else if' in the loop is not something I'd like to do.

    There's always the chance I'm overthinking it. If so, let me know.

    Thanks Harry

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Jul 30, 2018 @ 17:02
    Simon Dingley
    0

    There's no issue per se, I'd like to just have my custom models in the new partials I'm going to develop.

    OK, so what is stopping you? Nothing as far as I can see.

    I think where the problem will rely is when trying to use the loop that renders the page. Half the partials will be cusom models and half of them as I showed you before.

    Still no problem here as far as I can see, old ones stay as they are, news ones can be implemented with strongly type models if you choose.

    I'll have to render each partial with its model then, using @Html.RenderPartial and passing the model. Is there a more compact way I can do that?

    Adding tons of 'else if' in the loop is not something I'd like to do.

    I'm still not sure I am understanding but I'll have a stab based on my interpretation. You don't want to add if/else statements for each of the legacy doctypes you want to render from your nested content property as well as adding them for your new views? You don't need to do it for all of them, just handle the new ones you are adding as you will need to make sure you pass your new models into the views. If I was doing this I would use a switch statement and simply provide a default case to handle all the legacy doctypes. Something roughly like this:

    foreach (var item in Model.GetPropertyValue<IEnumerable<IPublishedContent>>("collection"))
    {   
            var partialViewName = "_" + item.DocumentTypeAlias;
    
            switch (item.DocumentTypeAlias)
            {
                case "newDocType1":
                    {
                        @Html.Partial(partialViewName, (NewModelOne)item, null)
                        break;
                    }
                case "newDocType2":
                    {
                        @Html.Partial(partialViewName, (NewModelTwo)item, null)
                        break;
                    }
                case "newDocType3":
                    {
                        @Html.Partial(partialViewName, (NewModelThree)item, null)
                        break;
                    }
                default:
                    @Html.Partial(partialViewName, (NewModelThree)item, null)
                    break;
            }
    }
    

    I've not tested this code but it should give you an idea.

    There's always the chance I'm overthinking it.

    Very possibly, but there is also the possibility that it has taken you longer trying to find a way to avoid writing this code than it would have to write it :)

    Cheers, Simon

  • Harry Spyrou 212 posts 604 karma points
    Jul 31, 2018 @ 08:51
    Harry Spyrou
    0

    Hi Simon,

    Thanks for your response but this wouldn't work for me as I've about 40 or 50 modules. I think your switch would have to have 40/50 cases, unless I'm somehow reading it wrong. Currently, I have this:

    @*items collection/Nested Content Editor*@
    @foreach (var item in Model.GetPropertyValue<IEnumerable<IPublishedContent>>("blabla"))
    {
        var partialViewName = "_" + item.DocumentTypeAlias;
        if (partialViewName == "_contactForm")
        {
            Html.RenderPartial("_ContactForm", new CustomerContactModel(Model, item));
        }
    
        else
        {
            @Html.Partial(partialViewName, item);
        }
    }
    

    I had this to separate the contact form (the only partial that had a custom model) and the rest of the partials. I guess I'll have to modify that to be something like:

    @*items collection/Nested Content Editor*@
    @foreach (var item in Model.GetPropertyValue<IEnumerable<IPublishedContent>>("collection"))
    {
    
        if (partialViewName == "_partialWithCustomModel")
        {
            Html.RenderPartial("_partialWithCustomModel", new MyCustomModel(Model, item));
        }
    
        else
        {
            @Html.Partial(partialWithoutCustomModel, item);
        }
    }
    
  • Simon Dingley 1474 posts 3431 karma points c-trib
    Jul 31, 2018 @ 09:01
    Simon Dingley
    0

    Thanks for your response but this wouldn't work for me as I've about 40 or 50 modules. I think your switch would have to have 40/50 cases, unless I'm somehow reading it wrong.

    When you say "modules" I'm assuming you mean document types (available in your nested content datatype) and their associated views? That is a lot however you only need to add case statements for the ones you want to use custom models with. However you implement this you have to have some sort of switching in order to check the document type and instantiate the correct models.

  • Harry Spyrou 212 posts 604 karma points
    Jul 31, 2018 @ 09:52
    Harry Spyrou
    0

    When you say "modules" I'm assuming you mean document types (available in your nested content datatype) and their associated views?

    You're right I probably worded it wrong. It's the project manager's fault! He's calling them that.

    Yes, basically each 'module' is one document type and has a partial with the same name. So the loop searches for a partial with the same name and an underscore. Do you see where the problem lies now?

    By the way thanks for your interest in this.

    Harry

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Jul 31, 2018 @ 09:56
    Simon Dingley
    0

    Do you see where the problem lies now?

    Haha, no! As I explained, "you only need to add case statements for the ones you want to use custom models with". From your responses so far I got the impression that this was a limited number of the overall collection?

  • Harry Spyrou 212 posts 604 karma points
    Jul 31, 2018 @ 10:08
    Harry Spyrou
    0

    No, it's actually all of them. I want to move the project to a more 'MVC' approach and move away from just repeating @Model.GetPropertyValue<string>("alias")

    in every single document type until the sun sets. So, I'd like to use Custom Models in every single one of them. (About 20 new ones). At the same time the previous/legacy remaining 30 of them will be left alone. I don't have time to touch them.

    So adding case statements won't work.

  • Harry Spyrou 212 posts 604 karma points
    Jul 31, 2018 @ 22:41
    Harry Spyrou
    0

    No, I was aiming for all of them. Slowly moving the legacy project to strongly typed models. To do that, I need to come up with a loop that I guess is like:

    @foreach (var partial in nestedContentCollection) {
        if (partialdoesn'thavecustommodel) 
        {
            @Html.Partial("bla")
        }
        else
        {
            @Html.RenderPartial(thePartial, itsmodel, etc)
        }
    

    }

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Aug 07, 2018 @ 07:11
    Simon Dingley
    0

    And where is itsmodel coming from? How will you determine the type of model pass into each view?

    Simon

  • Harry Spyrou 212 posts 604 karma points
    Aug 08, 2018 @ 12:03
    Harry Spyrou
    0

    Hi Simon,

    I was thinking of making something similar to what I've done with the loop that searches for the document Type Alias. So name the view models the same as the view.

    I also had another idea which is have one view model for everything as every module seems to be a textfield, an image and then something like a textarea and a link.

    If you're interested I'll update you on Friday when I'll actually get to have time to look into it.

    Regards Harry.

  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Aug 08, 2018 @ 14:01
    Frans de Jong
    2

    The biggest issue we ran into in the past is the propertyvalueconverters.

    For example: Our old code relies on commaseparated strings of id's and the new one gets a IENumerable<IPublishedContent>

    I always update the nuget package to the latest version and run through all the errors. Most of the time it takes less then half a day. After this I can work twice as fast because everything is strongly typed instead of string based.

    You'll win back the lost time in the end in my opinion.

  • Harry Spyrou 212 posts 604 karma points
    Aug 08, 2018 @ 17:29
    Harry Spyrou
    0

    Hi Frans,

    Thanks for the advice. I'll make sure to check if we have the same problem once I start.

    Harry

  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Aug 08, 2018 @ 18:40
    Frans de Jong
    0

    You can disable it if you want. Its in the update guide.

  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Oct 30, 2018 @ 08:40
    Frans de Jong
    0

    Hi Harry,

    Did you manage to merge the projects?

    Frans

  • Harry Spyrou 212 posts 604 karma points
    Oct 30, 2018 @ 09:32
    Harry Spyrou
    0

    Not exactly.

    Client after all wanted to continue on the old project and I had to keep going without custom models. I did test a few things though and it seems it would be simpler than I thought if I got to actually take the time to do it.

    Why?

  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Oct 30, 2018 @ 09:33
    Frans de Jong
    1

    Just curious, was looking in my list of open topics ;)

Please Sign in or register to post replies

Write your reply to:

Draft