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:
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.
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?
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.
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.
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 :)
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);
}
}
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.
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?
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?
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.
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)
}
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.
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.
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.
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:
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.
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:
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
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?
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.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
OK, so what is stopping you? Nothing as far as I can see.
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'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:
I've not tested this code but it should give you an idea.
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
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:
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:
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.
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
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?
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.
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:
}
And where is
itsmodel
coming from? How will you determine the type of model pass into each view?Simon
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.
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.
Hi Frans,
Thanks for the advice. I'll make sure to check if we have the same problem once I start.
Harry
You can disable it if you want. Its in the update guide.
Hi Harry,
Did you manage to merge the projects?
Frans
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?
Just curious, was looking in my list of open topics ;)
is working on a reply...