bind data to grid view in MVC? [Resolved]

Posted by Mandapallishree under ASP.NET on 7/29/2013 | Points: 10 | Views : 1368 | Status : [Member] | Replies : 2
Hi,

Im new to MVC.
Can you please tell me how i can bind data to gridview from database and perform crud operations?

Thanks,
Bhavya.




Responses

Posted by: Kmandapalli on: 7/29/2013 [Member] Silver | Points: 50

Up
0
Down

Resolved
Hi,

Step 1:

In the controller, write an Action method (Products) and write the following code:
public ActionResult Products()
{
var products = from p in db.ProductsMvcs
select new
{
ProductId = p.ProductId,
ProductName = p.ProductName,
ProductDiscount = p.ProductDiscount,
MRP = p.MRP,
TaxType = p.TaxType,
Tax = p.Tax
};
return View(products.ToList());
}

Here ProductsMvc is my table and im retreiving the data from that table.

Step 2:
Create a view (Products) for the ActionResult method and write the following code:
@{
ViewBag.Title = "Products";
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<style type="text/css">
.gridTable
{
margin: 5px;
padding: 10px;
border: 1px #c8c8c8 solid;
border-collapse: collapse;
min-width: 550px;
background-color: #fff;
color: #fff;
}
.gridHead th
{
font-weight: bold;
background-color: #000000;
color: #fff;
padding: 10px;
}
.gridHead a:link, .gridHead a:visited, .gridHead a:active, .gridHead a:hover
{
color: #FFBBFF;
}
.gridHead a:hover
{
text-decoration: underline;
}
.gridTable tr.gridAltRow
{
background-color: #efeeef;
}
.gridTable tr:hover
{
background-color: #FFCCCC;
}
.gridAltRow td
{
padding: 10px;
margin: 5px;
color: #333;
}
.gridRow td
{
padding: 10px;
color: #333;
}
.gridFooter td
{
padding: 10px;
background-color: #c7d1d6;
color: #999;
font-size: 12pt;
text-align: center;
}
.gridFooter a
{
font-weight: bold;
color: #333;
border: 1px #333 solid;
}
.</style>
</head>
<body>
<script src="../../Scripts/jquery-1.3.2-vsdoc2.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$(".add").live("click", function () {

var existrow = $('.save').length;
if (existrow == 0) {
var index = $("#DataTable tbody tr").length + 1;
var ProductId = "ProductId" + index;
var ProductName = "ProductName" + index;
var ProductDiscount = "ProductDiscount" + index;
var MRP = "MRP" + index;
var TaxType = "TaxType" + index;
var Tax = "Tax" + index;
var Save = "Save" + index;
var Cancel = "Cancel" + index;

var tr = '<tr class="alternate-row"><td><span> <input id="' + ProductName + '" type="text" /></span></td>' +
'<td><span> <input id="' + ProductDiscount + '" type="text"/></span></td>' +
'<td><span> <input id="' + MRP + '" type="text" /></span></td>' +
'<td><span> <input id="' + TaxType + '" type="text" /></span> </td>' +
'<td><span> <input id="' + Tax + '" type="text"/></span></td>' +
'<td><a href="#" id="' + Save + '" class="save"><img src="@Url.Content("~/Content/Images/save1.jpg")", alt="Save" /></a></td>' +
'<td><a href="#" id="' + Cancel + '" class="icancel"><img src="@Url.Content("~/Content/Images/cancell.jpg")", alt="Save" /></a></td>' +
'</tr>';
$("#DataTable tbody").append(tr);
}
else {
alert('First Save your previous record !!');
}
});

$(".icancel").live("click", function () {
var flag = confirm('Are you sure to cancel');
if (flag) {
$(this).parents("tr").remove();
}
});

$(".delete").live("click", function () {
var str = $(this).attr("id").split("_");
id = str[1];
var flag = confirm('Are you sure to delete ??');
if (id != "" && flag) {
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: '@Url.Action("DeleteUser", "Home")',
data: { "id": id },
dataType: "json",
beforeSend: function () { },
success: function (data) {
if (data.result == true) {
$("#Update_" + UserName).parents("tr").remove();
}
else {
alert('There is some error');
}
},
error: function () { location.reload(true); }
});

}
});

$(".save").live("click", function () {
var id = $("#DataTable tbody tr").length;
var ProductId = $("#ProductId" + id).val();
var ProductName = $("#ProductName" + id).val();
var ProductDiscount = $("#ProductDiscount" +

Kavya Shree Mandapalli

Mandapallishree, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mandapallishree on: 7/29/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,

Thank you, I used your code and its working fine..

Thanks,
Bhavya

Mandapallishree, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response