This article will show how you can bind an mvc WebGrid using
entity framework using c#.net. In this I have used entity framework and sql
server database with linq query to bind the webgrid.
Some of my previous articles are as follows: Responsive
Grid Design Example Using Css in Asp.Net MVC,HTML, Bind
jQuery DatePicker Calendar In MVC WebGrid and Retrive Value Using Asp.net MVC,
C#.Net, Bind
DropDownList In MVC WebGrid and Retrive Value Using Asp.net MVC, C#.Net, FileUpload
Control Inside WebGrid To Upload File In Asp.net MVC Using C#.Net, Auto
Increment Row Value In Asp.net MVC WebGrid Using C#.Net, Code
to Dynamically Bind Image To Asp.net MVC WebGrid, How
to Add Email Id Hyperlink or Mailto Link in Asp.net MVC WebGrid, MVC
WebGrid Custom paging With Page no and Shorting Data Using C#.Net, How
to Bind Data to Webgrid in ASP.net MVC Using C#.Net.
For this article first we will create a new mvc application.
Now we will create a table in sql database.
Now open your project and add an entity file.
Now we will create the model.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication1.Models
{
public class ProductDetailModel
{
public
List<Product>
ProductList { get; set;
}
}
public class Product
{
public
int ID { get;
set; }
public
string ProductName { get;
set; }
public
string ProductDetail { get; set; }
public
int CurrentStock { get;
set; }
}
}
|
After this add a controller file and add the below code to
controller.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI;
namespace MvcApplication1.Controllers
{
public class HomeController
: Controller
{
//
// GET:
/Home/
[HttpGet]
public
ActionResult Index()
{
ProductDetailModel
objproductdetailmodel = new ProductDetailModel();
objproductdetailmodel.ProductList
= ProductData();
return
View(objproductdetailmodel);
}
///
/// This finction is used for providing the data.
/// we will bind this data
/// to diaply in grid format.
/// You can get data from data base and
/// thee put into collection
///
public List<Product> ProductData()
{
List<Product> objProduct = new List<Product>();
DemoEntities objDemoEntities = new DemoEntities();
var productItem = from data in objDemoEntities.ProductMasters
select data;
foreach (var item in productItem)
{
objProduct.Add(new Product
{
ID = item.Id,
ProductName = item.ProductName,
ProductDetail = item.ProductDetail,
CurrentStock = (int)item.CurrentStock
});
}
return objProduct;
}
}
}
|
In above code I have retrieved the data from the database
and assign it to the model.
Now create the view and add the below code into the view.
@model MvcApplication1.Models.ProductDetailModel
@{
ViewBag.Title = "Bind
WebGrid With Entity Framework in Asp.Net MVC Using C#.Net";
}
<style>
table, td, th
{
border:
1px solid green;
border-collapse:
collapse;
}
th
{
border:
1px solid black;
background-color:
green;
color:
white;
}
</style>
@using (@Html.BeginForm("Index", "Home"))
{
var grid
= new WebGrid(Model.ProductList,
canSort: false);
<div>
@grid.GetHtml(columns:
grid.Columns
(
grid.Column("ID", "ID"),
grid.Column("ProductName", "Product Name"),
grid.Column("ProductDetail", "Product Detail"),
grid.Column("CurrentStock", "Current Stock")
), mode: WebGridPagerModes.Numeric)
</div>
}
|
Now we have done run the application to check the output.
0 comments:
Please let me know your view