Using MVC Grid In MVC Application

There are so many grids are available in the industries, most of them are useful. Here we are going to use a grid called MVC grid, which uses bootstrap and jQuery. We will create some dynamic data using list first, once it is done, we will send this data to the MVC grid. Sounds good? I hope you will like this article.

Download Source Code

  • Using MVC Grid In MVC


  • Background

    I have been working with the Grid controls for a long time. So far I have worked with jQX Grid, jQ Grid, jQuery Datatables, Pivot tables, KO grid, etc. It is always interesting to work with some controls. I always loved it. Recently, I worked with MVC grid. So I thought of sharing that experience with you all.

    Create an MVC application

    Firstly, we will start with creating an MVC application. Open your Visual Studio, then click File, New, then Project. Name your project.

    Install MVC Grid

    The next step we are going to do is, installing the MVC grid to our project. To install, please right click your solution and click on Manage NuGet Packages.

    Select NuGet Package

    Now you can see a new window, please search for MVC grid in the search box. And then click Install.

    Install_MVC_Grid_To_Project

    Once you installed, you can see there is a new reference file added(GridMVC), you can also notice that two views are created (_Grig.cshtml,_GridPager.cshtml) and one CSS file and some scripts. Now we will move to our next step.

    Dependencies

    As I said before, MVC grid uses bootstrap for design. So the next thing we need to is to install bootstrap in our project. For that, go to NuGet packages again and search for bootstrap.

    Install_Bootstrap_To_Project

    You can see some new CSS files and scripts added to our project. So the set up has been done. Now what we need to do is to move on the coding part.

    Create a controller

    Now we can create a new controller, in my case I created a controller ‘HomeController’. In my controller I am going to call a model action which will return some dynamic data. See the code below.
    1. public class HomeController: Controller  
    2. {  
    3.     //  
    4.     // GET: /Home/  
    5.     public ActionResult Index()  
    6.     {  
    7.         Test t = new Test();  
    8.         var myList = t.GetData();  
    9.         return View(myList);  
    10.     }  
    11. }  
    As you can see I am creating an instance of my model Test, now we will create our model class. Shall we?

    Create Model

    I have created a model class with the name Test. Here, I am creating some data dynamically using a for loop and assign those values to a list. Please see the codes below.
    1. namespace AsyncActions.Models  
    2. {  
    3.     public class Test  
    4.     {  
    5.         public List < Customer > GetData()  
    6.         {  
    7.             try  
    8.             {  
    9.                 List < Customer > cst = new List < Customer > ();  
    10.                 for (int i = 0; i < 100; i++)  
    11.                 {  
    12.                     Customer c = new Customer();  
    13.                     c.CustomerID = i;  
    14.                     c.CustomerCode = "CST" + i;  
    15.                     cst.Add(c);  
    16.                 }  
    17.                 return cst;  
    18.             }  
    19.             catch (Exception)  
    20.             {  
    21.                 throw new NotImplementedException();  
    22.             }  
    23.         }  
    24.     }  
    25.     public class Customer  
    26.     {  
    27.         public int CustomerID  
    28.         {  
    29.             get;  
    30.             set;  
    31.         }  
    32.         public string CustomerCode  
    33.         {  
    34.             get;  
    35.             set;  
    36.         }  
    37.     }  
    38. }  
    As you can see I am creating a list of type Customer. Is that fine? Now what is pending? Yes, a view.

    Create a strongly typed view

    Now we are going to create a strongly typed view.

    Create_Strongly_Typed_View

    When you create a view as strongly typed view, your view header will be as follows. @model List,

    So our view is ready, now we can do some codes in our view to populate our grid. Are you ready? First thing is you need to include the needed references to our view, you can do this in the file called Layout.cshtml. Here I am going to add those references directly to the view.
    1. <link href="~/Content/bootstrap-theme.css" rel="stylesheet" />  
    2. <link href="~/Content/bootstrap.css" rel="stylesheet" />  
    3. <script src="~/Scripts/jquery-2.1.3.min.js"></script>  
    4. <link href="~/Content/Gridmvc.css" rel="stylesheet" />  
    5. <script src="~/Scripts/gridmvc.min.js"></script>  
    Add the grid namespace

    You can add the grid namespace as follows.

    @using GridMvc.Html

    Next thing is to add grid implementation.

    MVC Grid Implementation

    To add a MVC grid as our requirement, you need to add the codes as below.
    1. @Html.Grid(Model).Columns(columns =>  
    2. {  
    3.     columns.Add(foo => foo.CustomerID).Titled("Customer ID").SetWidth(50).Sortable(true).Filterable(true);  
    4.     columns.Add(foo => foo.CustomerCode).Titled("Customer Code").SetWidth(50).Sortable(true).Filterable(true);  
    5. }).WithPaging(20)  
    As you can see we are using the columns Customer.CustomerID and Customer.CustomerCode.

    Output

    output

    Add more Grid features
    • To set the paging we can use the option WithPaging(20).
    • To add title we can use Titled property.
    • To set width we can use SetWidth property.
    • To set sort we can use Sortable property.
    • To set filter we can use Filterable property.
    You can always see the additional options here .

    Conclusion

    Did I miss anything that you may think which is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.

    Your turn. What do you think?

    A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.

    Please see this article in my blog here.

    Up Next
      Ebook Download
      View all
      Learn
      View all