I have just started having a play with models builder and I was wondering if anyone could help with a some questions I have to ensure i am understanding and following best practices
Setup
I have a homepage doc type and on the homepage template (view) i have an about section partial.
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.HomePage>
@using ContentModels = Umbraco.Web.PublishedContentModels;
@using Umbraco.Web.PublishedContentModels;
@{
Layout = "Layout.cshtml";
var aboutModel = Model.Content.Descendant<AboutSection>();
}
<!--render partials for each section on the home page-->
@{
if (aboutModel != null)
{
Html.RenderPartial("HomePage/AboutSection", aboutModel);
}
}
My first question is, is the way that i am using partials with models builder the correct approach e.g. using var aboutModel = Model.Content.Descendant
Secondly before I started using models builder I used to retrieve a crop Url using the following syntax
@Model.Content.GetCropUrl("image", "800x540")
On the aboutSection document type i have declared a image cropper property (alias image) and as you can see in my code above (var test) i have been attempted to retrieve the crop Url from that strongly typed property. The only way i can get it is by using the following but i am passing in "image", is there a better way?
var test2 = Model.GetCropUrl("image", "328x407");
Lastly, before models builder i used the hasValue method a lot so that i can include fallbacks e.g.
Being honest, there isn't much difference except the above is also check for "" and " ", as well as null.
I think it is just down to personal preference. The .HasValue() extension method was useful while using dynamic properties and it returned an Object rather than a Type. Seeing as you can now use strongly typed models, normal null/value checking should be fine I guess.
I do stand to be corrected on all of the above if someone with more knowledge would like to add their 2 cents!
My first question is, is the way that i am using partials with models
builder the correct approach e.g. using var aboutModel =
Model.Content.Descendant(); in the homepage and passing the model
through?
Yep, looks exactly right to me. You can pass a model to a partial, and that's the way to do it.
One tip - you can add namespaces to the web.config in your /Views/ folder (not your main web.config). So you can add in:
Advice when using models builder with partials
Hi,
I have just started having a play with models builder and I was wondering if anyone could help with a some questions I have to ensure i am understanding and following best practices
Setup
I have a homepage doc type and on the homepage template (view) i have an about section partial.
then my partial looks as follows
My first question is, is the way that i am using partials with models builder the correct approach e.g. using var aboutModel = Model.Content.Descendant
Secondly before I started using models builder I used to retrieve a crop Url using the following syntax
On the aboutSection document type i have declared a image cropper property (alias image) and as you can see in my code above (var test) i have been attempted to retrieve the crop Url from that strongly typed property. The only way i can get it is by using the following but i am passing in "image", is there a better way?
Lastly, before models builder i used the hasValue method a lot so that i can include fallbacks e.g.
How can i use this with strongly typed properties e.g.
I was expecting to see something like Model.Image.Value or something similar? I been getting around this by using != null (which is the same right?)
Any advice would be great, it would really help clear some things up :)
Thanks Paul
Hi Paul,
From what I have been told, for your first question, that is the correct way of doing it. Call Descendant but strongly typing it to what you want.
For your second question, I'd say using:
Would be the way I'd go about it or alternatively doing it this way:
Depends on which you prefer, the second way you are keeping it strongly typed but it's an extra line of code in your view.
Maybe there is a better way of doing this all together!
For your third point, using != null is okay and is the same as .HasValue(). However, you can use alternative extension methods like so:
Being honest, there isn't much difference except the above is also check for "" and " ", as well as null.
I think it is just down to personal preference. The .HasValue() extension method was useful while using dynamic properties and it returned an Object rather than a Type. Seeing as you can now use strongly typed models, normal null/value checking should be fine I guess.
I do stand to be corrected on all of the above if someone with more knowledge would like to add their 2 cents!
Cheers, Mike
Yep, looks exactly right to me. You can pass a model to a partial, and that's the way to do it.
One tip - you can add namespaces to the
web.config
in your/Views/
folder (not your main web.config). So you can add in:That way you don't need to keep adding it to every view or partial.
Hi both,
Thanks for the responses, it would appear that my understanding is not to far away!
Really appreciate you both taking the time out to provide detailed information!
Many thanks Paul
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.
Continue discussion