Bind GridView Without Database In MVC 3 C#

Here I haven’t used any database, we manually feed that data into our Controller, so it’s a kind of binding gridview without any database.

Step 1: Open Visual Studio 2010, go to the New Project, then Visual C#. After that click Web, select ASP.NET MVC3 Web Application and click OK.

MVC3 Web Application

Step 2: After clicking OK, new ASP.NET MVC3 Project window will open, there you have you choose Internet Application and in View engine dropdown chose Razor.

Chose Razor

Step 3: After clicking OK, you will see something like the following image in Solution Explorer, you need to look out for Model, Controller and View Folders, that are the main files in MVC, others are too but these are main files.

Solution Explorer

In Model

Step 4: Right Click on Models, then Add New Item and Add a Class File [Class.cs] Name it Student.cs. Write the following code in Student.cs file.

Student.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.   
  6. namespace MvcApplication1.Models  
  7. {  
  8.     public class Students  
  9.     {  
  10.   
  11.         public int Std_ID { getset; }  
  12.         public string Std_Name { getset; }  
  13.         public string Std_City { getset; }  
  14.   
  15.   
  16.   
  17.     }  
  18. }  
In Controller

Step 5: Open Controller, find HomeController.cs file there and open it, then write the following code. Don’t forget to add namespace of model.

HomeController.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using MvcApplication4.Models;  
  7.   
  8. namespace MvcApplication4.Controllers  
  9. {  
  10.     public class HomeController : Controller  
  11.     {  
  12.         public ActionResult Index()  
  13.         {  
  14.             Students stdnt = new Students();  
  15.             List<Students> stdnt1 = new List<Students>();  
  16.   
  17.   
  18.             stdnt.Std_ID = 1;  
  19.             stdnt.Std_Name = "Nilesh";  
  20.             stdnt.Std_City = "Rajkot";  
  21.             stdnt1.Add(stdnt);  
  22.   
  23.   
  24.             stdnt = new Students();  
  25.             stdnt.Std_ID = 2;  
  26.             stdnt.Std_Name = "Purnima";  
  27.             stdnt.Std_City = "Ahmedabad";  
  28.             stdnt1.Add(stdnt);  
  29.   
  30.   
  31.             stdnt = new Students();  
  32.             stdnt.Std_ID = 3;  
  33.             stdnt.Std_Name = "Rinku";  
  34.             stdnt.Std_City = "Pune";  
  35.             stdnt1.Add(stdnt);  
  36.   
  37.   
  38.   
  39.             stdnt = new Students();  
  40.             stdnt.Std_ID = 4;  
  41.             stdnt.Std_Name = "Chandni";  
  42.             stdnt.Std_City = "Chennai";  
  43.             stdnt1.Add(stdnt);  
  44.   
  45.   
  46.   
  47.             stdnt = new Students();  
  48.             stdnt.Std_ID = 5;  
  49.             stdnt.Std_Name = "Indrajeet";  
  50.             stdnt.Std_City = "Mumbai";  
  51.             stdnt1.Add(stdnt);  
  52.   
  53.   
  54.   
  55.             stdnt = new Students();  
  56.             stdnt.Std_ID = 6;  
  57.             stdnt.Std_Name = "Suhag";  
  58.             stdnt.Std_City = "Mysore";  
  59.             stdnt1.Add(stdnt);  
  60.   
  61.   
  62.             var data = stdnt1;  
  63.   
  64.             return View(data);  
  65.   
  66.   
  67.         }  
  68.         public ActionResult About()  
  69.         {  
  70.             return View();  
  71.         }  
  72.     }  
  73. }  
In View

Step 6: Open View Folder, under that Home folder is there, Open the Index.cshtml file and write the following code:

Index.cshtml
  1. @model IEnumerable<MvcApplication1.Models.Students>  
  2.   
  3. @{  
  4.     ViewBag.Title = "Home Page";  
  5.     WebGrid grid = new WebGrid(Model);  
  6. }  
  7.   
  8. <h2>Binding Gridview in MVC3</h2>  
  9. <p>  
  10.     This is my First Binding Tutorial of Gridview in MVC3  
  11. </p>  
  12.   
  13. @grid.GetHtml(columns: new[]  
  14. {  
  15.   
  16.   
  17.     grid.Column("Std_ID"),  
  18.     grid.Column("Std_Name"),  
  19.     grid.Column("Std_City")  
  20.   
  21.   
  22. })  
Output

GridView

Hope you liked it. Have a good day, Thank your for reading.

Up Next
    Ebook Download
    View all
    Learn
    View all