Copied to clipboard

Flag this post as spam?

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


  • Dan 1288 posts 3921 karma points c-trib
    Nov 26, 2013 @ 13:02
    Dan
    0

    Razor structure - share 'helpers' amongst partials

    Hi,

    I'm pretty new to Razor and indeed MVC and am wondering how best to structure the site I'm building (Umbraco 6.1.6 MVC).

    In short, the question is 'can I share helpers between partials'? The longer question is whether I've set this up correctly:

    Basically the site contains some general text pages which contain some widgets, such as 'articles' widgets. The articles widgets may be of different configurations, so for example there may be a 'latest articles' widget which shows the 3 latest articles or an 'other articles in this category' or 'popular articles' widget which shows a different configuration of articles as a widget on the page.

    I've set this up initially using a master page for the general text page. I've then created a partial view for each configuration of widget. So I have a partial called 'Latest articles', another called 'Popular articles' etc. It works nicely, each partial has the task of returning a specific set of articles to the template. However, some of the partials use the same mark-up to render the articles they're returning. It would be nice to create some kind of global helper or to be able to set global variables which can be shared between the partials so I'm not repeating code.

    Am I going about this completely wrong? Is there a way to do something like this?

    Thanks for any pointers folks.

  • Jeavon Leopold 3074 posts 13631 karma points MVP 11x admin c-trib
    Nov 26, 2013 @ 22:03
    Jeavon Leopold
    100

    Hi Dan,

    Yes you can, you need to create and keep your helpers in the app_code folder, there is a tutorial on how to do it here

    However, I would recommend that you might consider instead creating a C# project which contains helper and extension methods to use in your Razor views, there is an example (and lots of helpful helpers) here

    Jeavon

  • Dan 1288 posts 3921 karma points c-trib
    Nov 28, 2013 @ 14:24
    Dan
    0

    Those links are really helpful, thanks Jeavon!

Please Sign in or register to post replies

Write your reply to:

Draft