Copied to clipboard

Flag this post as spam?

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


  • Joshua Walsh 30 posts 151 karma points
    Feb 24, 2015 @ 04:43
    Joshua Walsh
    0

    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:

    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.

    Thanks,

    Josh

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Feb 24, 2015 @ 05:27
    Jan Skovgaard
    0

    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

  • Joshua Walsh 30 posts 151 karma points
    Feb 24, 2015 @ 05:43
    Joshua Walsh
    0

    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:

    namespace clientname.Model
    {
        public class Master
        {
            public string Header { get; set; }
            public HtmlString BodyText { get; set; }
            public string MetaTitle { get; set; }
            public string MetaDescription { get; set; }
            public string MetaKeywords { get; set; }
            public IEnumerable<MenuItem> MainNavigation { get; set; }
        }
    }
    

    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

  • Joshua Walsh 30 posts 151 karma points
    Feb 24, 2015 @ 06:09
    Joshua Walsh
    0

    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.

  • Joshua Walsh 30 posts 151 karma points
    Feb 24, 2015 @ 06:35
    Joshua Walsh
    1

    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/

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Feb 24, 2015 @ 07:22
    Jan Skovgaard
    0

    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

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Feb 24, 2015 @ 12:13
    Jeroen Breuer
    0

    Hello,

    You could also have a look at the Hybrid Framework. It shows how MVC can be used with Umbraco.

    Jeroen

Please Sign in or register to post replies

Write your reply to:

Draft