Copied to clipboard

Flag this post as spam?

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


  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Dec 10, 2014 @ 11:04
    Lee Kelleher
    0

    Can you use an MVC Controller with Macro?

    Is it possible to use an MVC controller (either regular MVC or SurfaceController) with a Macro View Partial?

    My initial check found that it wasn't possible, but I'm curious if others have figured out a way to achieve this?

    Thanks,
    - Lee

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Dec 10, 2014 @ 13:51
    Dave Woestenborghs
    100

    What I usually do is create a macro view partial and call my surface controller action from that.

    See this example :

    @using Umbraco.Web.Models
    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        int root = int.Parse(Model.GetParameterValue<string>("root", "0"));
    
        var categories = new List<int>();
        var productions = new List<int>();
    
        var paramCategories = Model.GetParameterValue<string>("categories", "");
        if (!string.IsNullOrEmpty(paramCategories))
        {
            paramCategories.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ForEach(x => categories.Add(int.Parse(x)));
        }
    
        var paramProductions = Model.GetParameterValue<string>("productions", "");
        if (!string.IsNullOrEmpty(paramProductions))
        {
            paramProductions.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ForEach(x => productions.Add(int.Parse(x)));
        }
    
        var number = int.Parse(Model.GetParameterValue<string>("numberitems", "7"));
    }
    
    @Html.Action("NewsGridList","NewsSurface",new RouteValueDictionary(new {  categories = categories, productions = productions, rootId = root, numberItems = number} ))

    Dave

  • Charles Afford 1163 posts 1709 karma points
    Dec 10, 2014 @ 16:36
    Charles Afford
    0
  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Dec 10, 2014 @ 17:12
    Lee Kelleher
    0

    Thanks Dave. As far as I can tell the only way is to call a child-action using Html.Action.

    Ideally I'd like to avoid using inline code inside the View, but I don't think that is possible with the Macro View Partial.

    For reference, I checked the core source-code for PartialViewMacroEngine, which calls its own controller (PartialViewMacroController) ... which suggests that it might be possible (someday) to have custom controllers for Macro View Partials.

    Food for thought :-)

    Thanks again!

    Cheers,
    - Lee

Please Sign in or register to post replies

Write your reply to:

Draft