Search code examples
asp.net-mvc-5mvcgrid

While Create gridview using grid.Mvc6 showing error


In my project i try to add grid but its showing following error 'HtmlHelper>' does not contain a definition for 'Grid' and the best extension method overload 'MvcGridExtensions.Grid(IHtmlHelper, IEnumerable)' requires a receiver of type 'IHtmlHelper'

Code:

@model IEnumerable<SCM_MVC.Models.XXXXX>
@using NonFactors.Mvc.Grid;

        @(Html
                .Grid(Model)
                .Build(columns =>
                {
                    columns.Add(model => model.select).Titled("Name");
                    columns.Add(model => model.Surname).Titled("Surname");
                    columns.Add(model => model.MaritalStatus).Titled("Marital status");

                    columns.Add(model => model.Age).Titled("Age");
                    columns.Add(model => model.Birthday).Titled("Birthday").Formatted("{0:d}");
                    columns.Add(model => model.IsWorking).Titled("Employed");
                })
                .Empty("No data found")
                .Filterable()
                .Sortable()
                .Pageable()
        )

What am doing wrong here?


Solution

  • The problem occurred because NonFactors.Grid.Mvc6 package uses IHtmlHelper interface derived from Microsoft.AspNetCore.Mvc.Rendering namespace, while the grid should use HtmlHelper class from System.Web.Mvc namespace, hence the package is incompatible with MVC 5.

    I suggest you to uninstall NonFactors.Grid.Mvc6 package (since it designed for ASP.NET Core MVC) and use NonFactors.Grid.Mvc5 package instead, by using Package Manager or console command:

    Uninstall-Package NonFactors.Grid.Mvc6
    Install-Package NonFactors.Grid.Mvc5
    

    Note:

    You can compare the contents of Grid extension method in MVC 5 and Core MVC version, which proves different namespace usage.

    Related issue:

    MVC:- Integration of mvc6.grid