Copied to clipboard

Flag this post as spam?

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


  • P 32 posts 122 karma points
    Feb 25, 2016 @ 11:34
    P
    0

    How to pass data to Grid rendering code

    Hi everyone,

    The Bootstrap Grid renderers don't do what I need, so I've written one of my own.

    I call it as follows:

    @Html.GetGridHtml(Model.Content, "content", "myGridRenderer")

    This is all working the way I want. However, I'd also like to pass in some additional arguments. There isn't a GetGridHtml overload that supports additional arguments, so I'm left wondering how best to pass them.

    Any suggestions?

    Thanks

    Pete

  • P 32 posts 122 karma points
    Feb 25, 2016 @ 15:38
    P
    0

    In the end, I extended the HtmlHelper in the same way as Core does. The gridrendering code now has access to a GridContext as well as the Grid itself.

    public class GridContext<T>
    {
        public IPublishedContent ContentItem { get; set; }
        public string PropertyAlias { get; set; }
        public string Framework { get; set; }
        public T Args { get; set; }
    
        public GridContext(IPublishedContent contentItem, string propertyAlias, string framework, T args)
        {
            ContentItem = contentItem;
            PropertyAlias = propertyAlias;
            Framework = framework;
            Args = args;
        }
    }
    
    public class GridModel<T>
    {
        public GridContext<T> Context { get; set; } 
        public object Grid { get; set; }
    
        public GridModel(GridContext<T> context, object grid)
        {
            Context = context;
            Grid = grid;
        }
    }
    
    public static class GridTemplateExtensions
    {        
        public static MvcHtmlString GetGridHtml<T>(this HtmlHelper html, IPublishedContent contentItem, string propertyAlias, string framework, T args)
        {
            Mandate.ParameterNotNullOrEmpty(propertyAlias, "propertyAlias");
    
            var gridModel = new GridModel<T>(new GridContext<T>(contentItem, propertyAlias, framework, args), null);
    
            var view = "Grid/" + framework;
            var prop = contentItem.GetProperty(propertyAlias);
            if (prop == null) throw new NullReferenceException("No property type found with alias " + propertyAlias);
    
            gridModel.Grid = prop.Value;
            var asString = gridModel.Grid as string;
            if (asString != null && string.IsNullOrEmpty(asString)) return new MvcHtmlString(string.Empty);
    
            return html.Partial(view, gridModel);
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft