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.
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.
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.
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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
-
- namespace MvcApplication1.Models
- {
- public class Students
- {
-
- public int Std_ID { get; set; }
- public string Std_Name { get; set; }
- public string Std_City { get; set; }
-
-
-
- }
- }
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 - using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using MvcApplication4.Models;
-
- namespace MvcApplication4.Controllers
- {
- public class HomeController : Controller
- {
- public ActionResult Index()
- {
- Students stdnt = new Students();
- List<Students> stdnt1 = new List<Students>();
-
-
- stdnt.Std_ID = 1;
- stdnt.Std_Name = "Nilesh";
- stdnt.Std_City = "Rajkot";
- stdnt1.Add(stdnt);
-
-
- stdnt = new Students();
- stdnt.Std_ID = 2;
- stdnt.Std_Name = "Purnima";
- stdnt.Std_City = "Ahmedabad";
- stdnt1.Add(stdnt);
-
-
- stdnt = new Students();
- stdnt.Std_ID = 3;
- stdnt.Std_Name = "Rinku";
- stdnt.Std_City = "Pune";
- stdnt1.Add(stdnt);
-
-
-
- stdnt = new Students();
- stdnt.Std_ID = 4;
- stdnt.Std_Name = "Chandni";
- stdnt.Std_City = "Chennai";
- stdnt1.Add(stdnt);
-
-
-
- stdnt = new Students();
- stdnt.Std_ID = 5;
- stdnt.Std_Name = "Indrajeet";
- stdnt.Std_City = "Mumbai";
- stdnt1.Add(stdnt);
-
-
-
- stdnt = new Students();
- stdnt.Std_ID = 6;
- stdnt.Std_Name = "Suhag";
- stdnt.Std_City = "Mysore";
- stdnt1.Add(stdnt);
-
-
- var data = stdnt1;
-
- return View(data);
-
-
- }
- public ActionResult About()
- {
- return View();
- }
- }
- }
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 - @model IEnumerable<MvcApplication1.Models.Students>
-
- @{
- ViewBag.Title = "Home Page";
- WebGrid grid = new WebGrid(Model);
- }
-
- <h2>Binding Gridview in MVC3</h2>
- <p>
- This is my First Binding Tutorial of Gridview in MVC3
- </p>
-
- @grid.GetHtml(columns: new[]
- {
-
-
- grid.Column("Std_ID"),
- grid.Column("Std_Name"),
- grid.Column("Std_City")
-
-
- })
Output Hope you liked it. Have a good day, Thank your for reading.