Articles in this section
Category / Section

How to create custom HtmlHelper for the ASP.NET MVC Grid?

1 min read

You can create custom helper to the Grid by using GridPropertiesBuilder. The following code example illustrates how to create MyGrid custom helper for the ASP.NET MVC Grid.

MVC

[Controller]
    public static class Extension
    {
        static Extension()
        {
            EssentialJavaScript.MetadataProvider = new MvcModelMetadataProvider();
        }
        // To create helper using extension method
        public static GridPropertiesBuilder<object> MyGrid(this System.Web.Mvc.HtmlHelper hepl, string id)
        {
            var model = new GridProperties();  //To get the grid properties 
            var grid = new Grid<object>(id, model);
            return new GridPropertiesBuilder<object>(grid); //return grid properties builder 
        }
    }
 
  public class GridController : Controller
    {
        public ActionResult GridFeatures()
        {
            var DataSource = new NorthwindDataContext().OrdersViews.ToList();
            ViewBag.datasource = DataSource;
            return View();
        }
    }
 
 [View]
@(Html.MyGrid("GridData")
         .Datasource((IEnumerable<object>)ViewBag.datasource)
        .AllowPaging()
            .Columns(col =>
            {
                col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Width(75).Add();
                col.Field("CustomerID").HeaderText("Customer ID").Width(80).Add();
                col.Field("EmployeeID").HeaderText("Employee ID").TextAlign(TextAlign.Right).Width(75).Add();      col.Field("Freight").HeaderText("Freight").TextAlign(TextAlign.Right).Width(75).Format("{0:C}").Add();
                col.Field("OrderDate").HeaderText("Order Date").TextAlign(TextAlign.Right).Width(80).Format("{0:MM/dd/yyyy}").Add();
                col.Field("ShipCity").HeaderText("Ship City").Width(110).Add();
            })
)

The following screenshot displays the Grid with custom helper.

Grid with custom helper


Conclusion

I hope you enjoyed learning about how to create custom HtmlHelper for the ASP.NET MVC Grid.

You can refer to our ASP.NET MVC Grid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET MVC Grid example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied