It doesn't say where that code should actually go, so I created a Banner.cs file in the Models folder to hold the model code, then a Banner.cshtml file in Views/Partials for the view. I don't really have any business logic for the banner, it just displays values from the model, so I haven't made a Controller file yet.
When I try to display the partial view in my layout using @Html.Partial("Banner.cshtml", new Banner("@CurrentPage.bannerTitle")) I get the following error:
The type or namespace name 'Banner' could not be found (are you missing a using directive or an assembly reference?)
I tried putting the contents of Banner.cs in a namespace and then @using the namespace in my layout, but this gave me an error about missing a reference.
Is it by purpose you're creating your own model? Where does the model fetch data from? Is it external data you want to fetch? Or is it actually data that exist in the Umbraco content section? In that case it should not be necessary to make your own model.
My banner needs to be provided with a few properties. For starters I am just providing a Title property, but eventually it will need more. These properties are pulled from the Umbraco content section, but depending on which template is including the banner the properties will be pulled from different places.
Aaah, good you found the culprit. I thought about this too but was too slow to get back to my machine and post the suggestion before you figured it out yourself.
But it's also hard to figure out how people structure their projects these days - but you have the same approach as me it seems. Keeping the VS project seperated from the website :)
Where to put Models in Umbraco 7
Hi,
I'm still very new to Umbraco 7. I'm trying to create reusable components using Razor Partials, loosely following this guide https://our.umbraco.org/documentation/Reference/Mvc/forms/turorial-partial-views. I'm making a reusable banner element to start off with.
It doesn't say where that code should actually go, so I created a Banner.cs file in the Models folder to hold the model code, then a Banner.cshtml file in Views/Partials for the view. I don't really have any business logic for the banner, it just displays values from the model, so I haven't made a Controller file yet.
When I try to display the partial view in my layout using @Html.Partial("Banner.cshtml", new Banner("@CurrentPage.bannerTitle")) I get the following error:
I tried putting the contents of Banner.cs in a namespace and then @using the namespace in my layout, but this gave me an error about missing a reference.
Thanks,
Josh
Hi Joshua
Is it by purpose you're creating your own model? Where does the model fetch data from? Is it external data you want to fetch? Or is it actually data that exist in the Umbraco content section? In that case it should not be necessary to make your own model.
If it's on purpose then please try reading this article by Bo Mortensen about creating your own models, controllers etc. http://24days.in/umbraco/2013/creating-reusable-code-in-mvc-apps/
Hope this helps.
/Jan
Hi Jan, thanks for your reply.
My banner needs to be provided with a few properties. For starters I am just providing a Title property, but eventually it will need more. These properties are pulled from the Umbraco content section, but depending on which template is including the banner the properties will be pulled from different places.
I have decided against using Umbraco's MacroPartials system as a result of this discussion: https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/61500-How-can-I-use-Umbraco-to-write-good,-maintainable-code. Most importantly, I don't want my reusable code to be dependant on stuff in the database. (which is where Macro Parameters are specified)
I also want my Model to be strongly typed, which is why I'm making it myself.
In my Googling I saw that 24days.in post, but it didn't help me. Bo defines his model as follows:
I have tried doing the same, but my problem still remains that in my Razor Layout I have no way to instantiate this class.
Thanks,
Josh
Looks like someone else had the same issue: https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/59330-How-to-create-a-Partial-view-using-Visual-Studio-MVC
Worth noting that my model is in the same project as everything else.
Sorry for the triple post, but I found the issue. In my deployment from Visual Studio to my development environment I was not copying over my /bin/
Hi Joshua
Aaah, good you found the culprit. I thought about this too but was too slow to get back to my machine and post the suggestion before you figured it out yourself.
But it's also hard to figure out how people structure their projects these days - but you have the same approach as me it seems. Keeping the VS project seperated from the website :)
Happy coding.
/Jan
Hello,
You could also have a look at the Hybrid Framework. It shows how MVC can be used with Umbraco.
Jeroen
is working on a reply...