Is this code creating its own grid for MVC?

I'm working on a new project that has lots of grids. I see this code

 @Html.Grid(Model.Results).Columns(column =>

Open in new window


1. My initial thought was that this is Grid.MVC . I searched for
@using GridMvc.Html

Open in new window

, didn't find it in the code. I don't see any gridmvc.css or grid.mvc razor code that comes with downloading and installing Grid.MVC

This is an example of Grid.MVC Grid.MVC example

2. Since I didn't see anything related to Grid.MVC, I stepped thru the code and it seems like this code base is creating it's own grid and using @Html.Grid

This is what I see, some of the code. Just want to make sure I'm correct that the code has created it's own Grid

 a. In Razor    
 @Html.Grid(Model.Results).Columns(column =>

Open in new window

b. Traced "Grid" property to this code. So, this is custom code...Extension method to create a grid...it's not Grid.MVC...Correct?

public static class GridExtensions
    {
        private const string CouldNotFindView =
            "The view '{0}' or its master could not be found. The following locations were searched:{1}";

        /// <summary>
        /// Creates a grid using the specified datasource.
        /// </summary>
        /// <typeparam name="T">Type of datasouce element</typeparam>
        /// <returns></returns>
        public static IGrid<T> Grid<T>(this HtmlHelper helper, IEnumerable<T> dataSource) where T : class
        {
            return new Grid<T>(dataSource, helper.ViewContext);
        }

        /// <summary>
        /// Creates a grid from an entry in the viewdata.
        /// </summary>
        /// <typeparam name="T">Type of element in the grid datasource.</typeparam>
        /// <returns></returns>
        public static IGrid<T> Grid<T>(this HtmlHelper helper, string viewDataKey) where T : class
        {
            var dataSource = helper.ViewContext.ViewData.Eval(viewDataKey) as IEnumerable<T>;

            if (dataSource == null)
            {
                throw new InvalidOperationException(string.Format(
                                                        "Item in ViewData with key '{0}' is not an IEnumerable of '{1}'.", viewDataKey,
                                                        typeof(T).Name));
            }

            return helper.Grid(dataSource);
        }

        /// <summary>
        /// Defines additional attributes for the column heading.
        /// </summary>
        /// <returns></returns>
        public static IGridColumn<T> HeaderAttributes<T>(this IGridColumn<T> column, params Func<object, object>[] hash)
        {
            return column.HeaderAttributes(new Hash(hash));
        }

        /// <summary>
        /// Defines additional attributes for a grid.
        /// </summary>
        /// <returns></returns>
        public static IGridWithOptions<T> Attributes<T>(this IGridWithOptions<T> grid, params Func<object, object>[] hash)
            where T : class
        {
            return grid.Attributes(new Hash(hash));
        }

        /// <summary>
        /// Defines additional attributes for the cell.
        /// </summary>
        public static IGridColumn<T> Attributes<T>(this IGridColumn<T> column, params Func<object, object>[] hash)
        {
            return column.Attributes(x => new Hash(hash));
        }

        /// <summary>
        /// Associates custom attributes with every grid row.
        /// </summary>
        public static void RowAttributes<T>(this IGridSections<T> sections, Func<GridRowViewData<T>, IDictionary<string, object>> attributes) where T : class
        {
            sections.Row.Attributes = attributes;
        }

        public static IView TryLocatePartial(this ViewEngineCollection engines, ViewContext context, string viewName)
        {
            var viewResult = engines.FindPartialView(context, viewName);

            if (viewResult.View == null)
            {
                var locationsText = new StringBuilder();
                foreach (var location in viewResult.SearchedLocations)
                {
                    locationsText.AppendLine();
                    locationsText.Append(location);
                }

                throw new InvalidOperationException(string.Format(CouldNotFindView, viewName, locationsText));
            }

            return viewResult.View;
        }

        /// <summary>
        /// Renders the specified text at the start of every row instead of the default output.
        /// </summary>
        public static void RowStart<T>(this IGridSections<T> sections, Func<GridRowViewData<T>, string> rowStart) where T : class
        {
            sections.Row.StartSectionRenderer = (rowData, context) =>
            {
                context.Writer.Write(rowStart(rowData));
                return true;
            };
        }

        /// <summary>
        /// Renders the specified text at the end of every row instead of the default output.
        /// </summary>
        public static void RowEnd<T>(this IGridSections<T> sections, Func<GridRowViewData<T>, string> rowEnd) where T : class
        {
            sections.Row.EndSectionRenderer = (rowData, context) =>
            {
                context.Writer.Write(rowEnd(rowData));
                return true;
            };
        }

        /// <summary>
        /// The HTML that should be used to render the header for the column. This should include TD tags.
        /// </summary>
        /// <param name="column">The current column</param>
        /// <param name="header">The format to use.</param>
        /// <returns></returns>
        [Obsolete("Please use the overload of column.Header that accepts a razor template. Eg, column.For(x => x.Foo).Header(@<div>Custom markup here</div>)")]
        public static IGridColumn<T> Header<T>(this IGridColumn<T> column, string header) where T : class
        {
            return column.Header(x => header);
        }

        /// <summary>
        /// Specifies custom attributes for the header row.
        /// </summary>
        public static void HeaderRowAttributes<T>(this IGridSections<T> sections, IDictionary<string, object> attributes) where T : class
        {
            sections.HeaderRow.Attributes = x => attributes;
        }

        /// <summary>
        /// Specifies that the grid's columns should be automatically generated from the public properties on the model object.
        /// </summary>
        public static IGridWithOptions<T> AutoGenerateColumns<T>(this IGrid<T> grid) where T : class
        {
            var autoColumnBuilder = new AutoColumnBuilder<T>(ModelMetadataProviders.Current);

            return grid.Columns(columnBuilder =>
            {
                ICollection<GridColumn<T>> columns = columnBuilder;

                foreach (var column in autoColumnBuilder)
                {
                    columns.Add(column);
                }
            });
        }

        /// <summary>
        /// Executes a delegate that can be used to specify custom HTML to replace the built in rendering of the start of the row.
        /// </summary>
        /// <param name="grid">The grid</param>
        /// <param name="template">Razor template to use.</param>
        public static IGridWithOptions<T> RowStart<T>(this IGridWithOptions<T> grid, Func<T, object> template) where T : class
        {
            grid.Model.Sections.Row.StartSectionRenderer = (rowData, context) =>
            {
                context.Writer.Write(template(rowData.Item));
                return true;
            };
            return grid;
        }

        /// <summary>
        /// Executes a delegate that can be used to specify custom HTML to replace the built in rendering of the end of the row.
        /// </summary>
        /// <param name="grid">The grid</param>
        /// <param name="template">Razor template to use.</param>
        public static IGridWithOptions<T> RowEnd<T>(this IGridWithOptions<T> grid, Func<T, object> template) where T : class
        {
            grid.Model.Sections.Row.EndSectionRenderer = (rowData, context) =>
            {
                context.Writer.Write(template(rowData.Item));
                return true;
            };

            return grid;
        }
    }

Open in new window


This is more code, if it helps. "Column" property goes here

public class Grid<T> : IGrid<T> where T : class
    {
        private readonly ViewContext _context;
        private IGridModel<T> _gridModel = new GridModel<T>();

        /// <summary>
        /// The GridModel that holds the internal representation of this grid.
        /// </summary>
        public IGridModel<T> Model
        {
            get { return _gridModel; }
        }

        /// <summary>
        /// Creates a new instance of the Grid class.
        /// </summary>
        /// <param name="dataSource">The datasource for the grid</param>
        /// <param name="context"></param>
        public Grid(IEnumerable<T> dataSource, ViewContext context)
        {
            this._context = context;
            DataSource = dataSource;
        }

        /// <summary>
        /// The datasource for the grid.
        /// </summary>
        public IEnumerable<T> DataSource { get; private set; }

        public IGridWithOptions<T> RenderUsing(IGridRenderer<T> renderer)
        {
            _gridModel.Renderer = renderer;
            return this;
        }

        public IGridWithOptions<T> Columns(Action<ColumnBuilder<T>> columnBuilder)
        {
            var builder = new ColumnBuilder<T>();
            columnBuilder(builder);

            foreach (var column in builder)
            {
                if (column.Position == null)
                {
                    _gridModel.Columns.Add(column);
                }
                else
                {
                    _gridModel.Columns.Insert(column.Position.Value, column);
                }
            }

            return this;
        }

        public IGridWithOptions<T> Empty(string emptyText)
        {
            _gridModel.EmptyText = emptyText;
            return this;
        }

        public IGridWithOptions<T> Attributes(IDictionary<string, object> attributes)
        {
            _gridModel.Attributes = attributes;
            return this;
        }

        public IGrid<T> WithModel(IGridModel<T> model)
        {
            _gridModel = model;
            return this;
        }

        public IGridWithOptions<T> Sort(GridSortOptions sortOptions)
        {
            _gridModel.SortOptions = sortOptions;
            return this;
        }

        public IGridWithOptions<T> Sort(GridSortOptions sortOptions, string prefix)
        {
            _gridModel.SortOptions = sortOptions;
            _gridModel.SortPrefix = prefix;
            return this;
        }

        public override string ToString()
        {
            return ToHtmlString();
        }

        public string ToHtmlString()
        {
            var writer = new StringWriter();
            _gridModel.Renderer.Render(_gridModel, DataSource, writer, _context);
            return writer.ToString();
        }

        public IGridWithOptions<T> HeaderRowAttributes(IDictionary<string, object> attributes)
        {
            _gridModel.Sections.HeaderRowAttributes(attributes);
            return this;
        }

        [Obsolete("The Render method is deprecated. From within a Razor view, use @Html.Grid() without a call to Render.")]
        public void Render()
        {
            _gridModel.Renderer.Render(_gridModel, DataSource, _context.Writer, _context);
        }

        public IGridWithOptions<T> RowAttributes(Func<GridRowViewData<T>, IDictionary<string, object>> attributes)
        {
            _gridModel.Sections.RowAttributes(attributes);
            return this;
        }
    }

Open in new window

LVL 8
CamilliaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chinmay PatelChief Technical NinjaCommented:
Hi camillia,

Your assumption is right. That line Html.Grid is generating the Grid.

You can refer to another example to understand how to use it:
https://www.codeproject.com/Tips/597253/Using-the-Grid-MVC-in-ASP-NET-MVC

Regards,
Chinmay.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CamilliaAuthor Commented:
Yes,  it's generating the grid but that's not out of the box grid.mvc. I think it's custom code.
0
Chinmay PatelChief Technical NinjaCommented:
Of course not. It is a custom HTML Helper written for Grid.Mvc project.

Please check :
https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/views/creating-custom-html-helpers-cs

To understand custom HTML helper.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

CamilliaAuthor Commented:
Wonder why they just didn't use grid.mvc and wrote an html helper.

I'll look at the links. Thanks
0
Chinmay PatelChief Technical NinjaCommented:
HTML Helpers are shorthands to replace a bunch of code. So the Helper you are talking about is most probably, a short hand that renders Grid.MVC. I am not able to check the source code (Author has not moved his solution from CodePlex - might be an abandoned project) and I don't want to download the entire archive but If you debug it you will find that Html.Grid is actually part of Grid.MVC only.
0
CamilliaAuthor Commented:
Ah, let me see.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.