Firstly I am relatively new to Umbraco 7.4.3, so perhaps I am just trying to do this the wrong way? I am hoping the community can set me straight. All I really want in life is IntelliSense in Visual Studio for my DocumentTypes.
I am trying to use compositions in my DocumentTypes (Rather than master DocumentTypes, this seems to be the way forward in new versions). However I am unable to cast to an instance of the composition, meaning that partial views for that compoisition cause an exception.
More Detial:
DocumentTypes
- Compositions (Folder)
- Seo – DocType
- Home – DocType (which is using the Seo Composistion)
Templates
Layout
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
Layout = null;
}
<html>
<head>
@RenderSection("Head", required: false)
</head>
@RenderBody()
<body>
</body>
</html>
Home
@inherits Umbraco.Web.Mvc.UmbracoViewPage<Home>
@using Umbraco.Web.CompiledViewModels
@{
Layout = "Layout.cshtml";
}
@Section Head
{
@Html.Partial("Seo")
}
<h1>My new home page</h1>
Seo (Partial)
@inherits Umbraco.Web.Mvc.UmbracoViewPage<Seo>
@using Umbraco.Web.CompiledViewModels
<title>Model.Title</title>
However when I view the home page I get an error that the “Home” type cannot be converted to an “Seo” type.
Looking at the Models that have been created I can see that the “Seo” Model has a corresponding interface “ISeo” which is implemented by “Home”. So I am not quite sure why it cannot cast between these types.
I would like to use stronly typed PartialViews for my Compositions – Is this wrong? should I be using something else?
I have tried casting the Model myself, with no joy.
@Html.Partial("Seo", Model as Seo)
2
Answers
I think that in the frontend, whatever Model/Document Type you are on, is viewed as one document type, regardless of whatever comps it’s made from. There should be no reason to cast anything, as whatever properties are in the Seo doctype should be part of the model. At least that’s my understanding.
I don’t know how well ModelsBuilder works as part of the core, but AFAIK it’s what should be providing the strongly typed models (and IntelliSense). Have you checked to see if the Seo properties are not already part of the Home model?
EDIT
Sorry, didn’t really mean to put this as an answer, with all the uncertainties and even a question of my own. I’ll leave it here until someone tells me otherwise 😉
Because a DocType can have multiple compositions assigned to it they couldn’t use inheritance on the doc type for the compositions as it’s not possible to inherit from multiple classes in C#. Instead they create an interface as you mentioned in your post and then both the doctype class and the composition class implements it. Then if I remember correctly the properties on the doctype then call static methods on the compositions class to get the values. So instead of using the following in your partial, you should be able to use the interface:
Use the this instead: