Copied to clipboard

Flag this post as spam?

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


  • Rune Grønkjær 1371 posts 3102 karma points
    Jan 25, 2016 @ 09:14
    Rune Grønkjær
    1

    How to call GetGridHtml with HtmlHelper?

    Hi Guys,

    For some time I have been using the deprecated overloads of the GetGridHtml IPublishedContent extension. I was just wondering how I would change it to the correct one, that needs a HtmlHelper as parameter!?

    I'm using it in a data model, to ready the data for the view. Today it looks something like this:

    using System.Web.Mvc;
    using Umbraco.Core.Models;
    using Umbraco.Web;
    using Website.Extensions.Models.DocumentTypes.General;
    
    namespace Website.Extensions.Models.DocumentTypes {
      public class Page : ATemplateViewModel {
    
        public string Headline { get; private set; }
        public string Text { get; private set; }
        public MvcHtmlString Grid { get; private set; }
    
        public Page( IPublishedContent content )
          : base( content ) {
          Grid = content.GetGridHtml( "grid" );
          Headline = content.GetPropertyValue

    GetGridHtml i deprecated and Visual Studio tells me that its parameters should be: GetGridHtml(HtmlHelper, string)

    How would I do this here? The "Page" class will be instatiated in a controller, so I don't have the automatically whatever generated for the views.

    Cheers Rune

  • Anders Bjerner 487 posts 2989 karma points MVP 7x admin c-trib
    Jan 28, 2016 @ 09:13
    Anders Bjerner
    0

    From a MVC perspective, you normally wouldn't generate the HTML in the model. So given your current model, I would remove the Grid property, and then add a new one with the IPublishedContent (so it can be accessed from the view):

    public IPublishedContent Content { get; private set; }
    

    And then of course also set it in your constructor.

    With the Content property present, you can then do something like this in your view (where Html is already present):

    @Model.Content.GetGridHtml(Html, "grid")
    

    The line below gives the same result (just another way to do it):

    @Html.GetGridHtml(Model.Content, "grid")
    
  • Rune Grønkjær 1371 posts 3102 karma points
    Jan 28, 2016 @ 10:44
    Rune Grønkjær
    0

    Hi Anders,

    I see your point and I would never generate html in my model (Unless desperate). In this case though I'm not desperate, but I want a clean view, with NO knowledge of the IPublishedContent. I'm just filling my model properties with the data from the IPublishedContent object.

    That's why I'm trying to figure it out. How the h*ll to make a HtmlHelper in my model?! :D I don't see why that should even be necessary.

    /Rune

  • Rune Grønkjær 1371 posts 3102 karma points
    Jan 28, 2016 @ 10:50
    Rune Grønkjær
    0

    Looks like Umbraco does it like this

    private static HtmlHelper CreateHtmlHelper(object model)
            {
                var cc = new ControllerContext
                {
                    RequestContext = UmbracoContext.Current.HttpContext.Request.RequestContext
                };
                var viewContext = new ViewContext(cc, new FakeView(), new ViewDataDictionary(model), new TempDataDictionary(), new StringWriter());
                var htmlHelper = new HtmlHelper(viewContext, new ViewPage());
                return htmlHelper;
            }
    

    But warns like this: "This shouldn't need to be used but because the obsolete extension methods above don't have access to the current HtmlHelper, we need to create a fake one, unfortunately however this will not pertain the current views viewdata, tempdata or model state so should not be used"

    I guess I could just do it like that?! I don't need the current stuff.

    /Rune

  • Anders Bjerner 487 posts 2989 karma points MVP 7x admin c-trib
    Jan 28, 2016 @ 10:58
    Anders Bjerner
    0

    Yes, you can create a fake HtmlHelper. I was just about to find some examples for that ;) But I normally don't go down this path, so I can't tell if there are any consequences by doing so.

    Alternatively you can have a look at my Grid package. The main purpose of the package is to give you a strongly typed model for the grid - so rather than your Grid property being of the type MvcHtmlString, it could instead be of the type GridDataModel, you can then use in your view with full intellisense. But not knowing your project, this could involve some further work - so it's just a suggestion ;)

  • Rune Grønkjær 1371 posts 3102 karma points
    Jan 28, 2016 @ 12:10
    Rune Grønkjær
    0

    Thanks. I'm using Doc type grid editor in my grid, and creates strongly types objects based on the content delivered by that. So that part is covered. :)

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Jan 28, 2016 @ 11:25
    Douglas Robar
    0

    Hi, Rune,

    You've probably seen it, but this issue has some very helpful comments and a couple links that may help.

    http://issues.umbraco.org/issue/U4-6209

    cheers,
    doug.

  • Rune Grønkjær 1371 posts 3102 karma points
    Jan 28, 2016 @ 12:17
    Rune Grønkjær
    0

    Thanks Doug

    No I had not found that. Looks like they are planning to mess up my code even more :)

    I will take the discution there. There must be another way!

    /Rune

Please Sign in or register to post replies

Write your reply to:

Draft