Chiranjeevi
Chiranjeevi

Reputation: 171

How to Bind List to Grid in Asp.Net MVC

I am trying to bind database to Grid in MVC5 application, its my first attempt.

Created Model class, and Controller. My model is returning data table. and i would like to bind the data in the grid. Below is my controller code which will return list.

    List<SystemManagmentModel> fileStores = new List<SystemManagmentModel>();
    SystemManagmentModel fileStore = new SystemManagmentModel();
    fileStores.Add(fileStore);
    return View(fileStores);

I am able to add Model in the view @model IEnumerable

From here i am unable to understand how to create grid and how to bind this list.I have selected Razor view Engine.

can any body show some sample code to Bind grid options in MVC Thank you

Upvotes: 0

Views: 3900

Answers (1)

Jamie Rees
Jamie Rees

Reputation: 8183

Take a look at MVC Grid it is a life saver.

This is how you would use it:

@Html.Grid(Model).Columns(columns =>
{
columns.Add(x => x.Property).Titled("name of column header").Sortable(true);

columns.Add(x => x.DateTimeProperty).Titled("Release Date").SortInitialDirection(GridSortDirection.Descending)
    .Format("{0:dd/MM/yyyy HH:mm}")
    .ThenSortByDescending(x => x.DateTimeProperty);

columns.Add(x => x.Property3).Titled("Another Prop");

columns.Add(x => x.Id)
.Titled(string.Empty)
.Encoded(false)
.Sanitized(false)
.RenderValueAs(x => @<b> @Html.ActionLink("Delete", "DeleteAction", new { id = x.Id })</b>)
    .SetWidth(100);

}).Sortable(true)

Upvotes: 1

Related Questions