Copied to clipboard

Flag this post as spam?

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


  • Frederik T 242 posts 373 karma points
    Aug 15, 2022 @ 06:58
    Frederik T
    0

    Using GetGridHtml in a controller in Umbraco 10

    I have made a simple Api controller on my umbraco website, that simply searches some nodes for some properties and returns the content from those properties in json. One of the properties is an umbraco grid. Technically that is also json, and I could successfully get the content with some elaborate JObject querying. But from the view side there is access to a "GetGridHtml" helper function that does that for you. So my question is, how do I achieve that in a controller? I got the node as IPublishedContent which incidentally has that helper function, but it wants an IHtmlHelper which isn't available in this context. So what do I do?

    [HttpGet]
    [Route("api/myapp/version/")]
    public IActionResult GetLatestMyAppVersion()
    {
        var releaseNote = _umbracoHelper.ContentAtXPath("//releaseNoteList").First().Children().OrderByDescending(x => x.Value<DateTime>("releaseDate")).First();
        var ver = releaseNote.Value<string>("myAppReleaseVersion");
        var date = releaseNote.Value<DateTime>("releaseDate");
    
        var desc = releaseNote.GetGridHtml(?????, "mainContent", "Clean"); // this is where IHtmlHelper is needed
    
        return Ok(new { Application = "MyApp", Version = ver, ReleaseDate = date.ToString("yyyy-MM-dd"), Description = desc });
    }
    
  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Aug 15, 2022 @ 09:27
    Huw Reddick
    0

    HtmlHelper is not really meant to be used outside of views, you could try the below link for a solution

    https://github.com/aspnet/Mvc/issues/7321

  • Frederik T 242 posts 373 karma points
    Aug 15, 2022 @ 09:29
    Frederik T
    0

    Well ok, then what do I do instead? There is no proposed solution in the link you provided.

  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Aug 15, 2022 @ 15:57
    Huw Reddick
    0

    the initial post in the link proposes the solution you could try.

  • Frederik T 242 posts 373 karma points
    Aug 15, 2022 @ 20:53
    Frederik T
    0

    Ah, sorry, I think I understand it now. Short version is there is no method built into the Umbraco API where I can get the HTML helper? So I can not use GetGridHtml without generating one myself? Which seems really overkill and bad practice, am I right?

  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Aug 16, 2022 @ 10:49
    Huw Reddick
    0

    IHtmlHelper is a .net core interface and nothing to do with Umbraco, it is bad .Net practice to use IHmHelper outside of views. So without instantiating a fake one you can't use GetGridHtml in a controller or class.

    What exactly did you want to do with the html? Why not just return a partialView which could use the GetGridHtml?

  • Frederik T 242 posts 373 karma points
    Aug 16, 2022 @ 18:27
    Frederik T
    0

    Well Its not because I WANT to use ihtmlhelper, which is also why I was asking around here. As explained in the OP, I have made an API that fetches a few key details from some umbraco nodes and sends that data back as a simple piece of JSON. One of those properties from those nodes is an umbraco grid. That is stored as JSON and I can get the grid data in a JObject (not in the example I have given). As I was making it, it just seemed more correct to use GetGridHtml. But I understand now that is off the table. So either getting the grid json "manually" is the correct path, or there is some other more "clever" method.

  • Joshua Crittenden 14 posts 124 karma points
    Nov 09, 2022 @ 16:51
    Joshua Crittenden
    0

    Any solution? I'm needing the same thing. Exposing grid and/or blocklist in an API JSON return.

  • Bo Jacobsen 608 posts 2406 karma points
    Nov 09, 2022 @ 19:56
    Bo Jacobsen
    0

    The Grid content is already a json value. So you should just be able to pass it to the Description property?

Please Sign in or register to post replies

Write your reply to:

Draft