Copied to clipboard

Flag this post as spam?

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


  • Kris Janssen 210 posts 569 karma points c-trib
    Jun 21, 2017 @ 02:46
    Kris Janssen
    0

    Correct way to call GetGridHtml()

    Messing around with ModelsBuilder, I have been converting some old style CurrentPage views.

    While I was at it I decided to derive my views from UmbracoViewPage<MyNiceModel>

    The doctype for MyNiceModel contains a grid property called Content (a bit confusingly maybe) so I tried:

    @Model.GetGridHtml("content", "mygridpartial")
    

    That gives me a 'deprecated' warning though:

    Warning

    I therefore went with:

    @Html.GetGridHtml(Model, "content", "mygridpartial")

    However, that seems less elegant given that I now have to provide the Model property as a string value (so there goes one of the benefits of strongly typed models?).

    Thoughts welcome.

    EDIT

    To avoid confusion due to my poor choice of doctype property names and to increase educational value :) this is the problem:

    My Doctype has a property that is a grid, call it MyGridProperty

    There are two ways to get at it in templates (i.e. views) when using ModelsBuilder:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.MyDocType>
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    ...
    
    @Model.Content.MyGridProperty
    

    or if you go the route that V8 will ultimately go:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

    @Model.MyGridProperty
    

    In both cases however, MyGridProperty will return as type Newtonsoft.Json.Linq.JToken because of course, grid content is stored as JSON.

    That causes a problem in the sense that none of the overloads for @Html.GetGridHtml() take arguments other than IPublishedContent or alias strings as you can see here

    I can somehow understand why that it is so but then again a call like:

    @Html.GetGridHtml(Model, "MyGridProperty", "MyGridPartial")
    

    Takes away some of the advantages of strong typing (because you refer to the property in string form).

    Should the Grid related extension methods not be able to handle JToken arguments directly?

  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Jun 21, 2017 @ 08:15
    Frans de Jong
    0

    Can't you do: @Html.GetGridHtml(Model.Content.GridProperty, "mygridpartial") ?

  • Kris Janssen 210 posts 569 karma points c-trib
    Jun 21, 2017 @ 12:23
    Kris Janssen
    0

    I am using UmbracoViewPage for the template (the one that will be the new standard in v8) so you can omit the Content property of model and instead call @Model.someproperty.

    However, my property is called Content (hence my calling it confusing). I edited the original post so that is clarified a bit.

    As such, I cannot seem to call what you suggest, I.e. @Html.GetGridHtml(Model.someproperty, "some partial alias") because there are no overloads of GetGridHtml that take anything other than IPublishedContent as arguments...

  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Jun 21, 2017 @ 12:25
    Frans de Jong
    1

    Hmm, I have to look into that. I know I had something similar in a project.

  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Jun 28, 2017 @ 23:15
    Frans de Jong
    1

    I tried several ways but no luck. We use @Html.GetGridHtml(IPublishedContent, "propertyƄlias", "partialAlias") It seems to be the only way.

    It would be nice if we could have a @html.GetGridHtml(Model.Property, "partialAlias")

  • Kris Janssen 210 posts 569 karma points c-trib
    Jun 28, 2017 @ 23:19
    Kris Janssen
    0

    No worries, thanks for following up though.

    If you look at the source there is indeed no other way.

    Probably the Html extensions need to be amended with one that can handle the JSON or the model should return something else than it is doing now. I guess only HQ knows how they will go about it :)

  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Jun 28, 2017 @ 23:21
    Frans de Jong
    0

    Lol, Why are we awake :P

    But true. It is something that's overlooked since the implementation of modelsbuilder I think

  • Kris Janssen 210 posts 569 karma points c-trib
    Jun 28, 2017 @ 23:24
    Kris Janssen
    0

    Haha, I am actually working from North America for a while so for me the hour is still reasonable, but I know the feeling: digging into the inner workings of Umbraco can keep you awake :)

  • Robert J. Bullock 386 posts 405 karma points
    Oct 22, 2018 @ 16:53
    Robert J. Bullock
    0

    I just tripped across this very need. Trying to do everything using strongly typed models and this is a bit of a fly in the ointment.

  • Jeroen Vantroyen 54 posts 394 karma points c-trib
    Feb 10, 2020 @ 15:37
    Jeroen Vantroyen
    0

    If using ModelsBuilder, can't you use something like below?

    @Html.GetGridHtml(Model, MyDocType.GetModelPropertyType(c => c.MyGridProperty).Alias, "MyGridPartial")
    

    Looks convoluted, but it's strongly typed.

Please Sign in or register to post replies

Write your reply to:

Draft