@model IEnumerable<MVC_GridCrud.Models.Test>
@{
ViewBag.Title = "Index";
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<style type="text/css">
body{
font-family:Arial;
font-size:10pt;
}
.table{
border:1px solid #ccc;
border-collapse:collapse;
}
.table th{
background-color:#f7f7f7;
color:#333;
font-weight:bold;
}
.table th, .table td{
padding:5px;
border:1px solid #ccc;
}
</style>
</head>
<body>
<table id="tbltest" class="table" cellpadding="0" cellspacing="0">
<tr>
<th style="width:100px">Id</th>
<th style="width:150px">Name</th>
<th style="width:150px">Department</th>
<th style="width:150px">CreatedDate</th>
<th style="width:150px"></th>
</tr>
@foreach(MVC_GridCrud.Models.Test test in Model)
{
<tr>
<td class="Id">
<span>@test.Id</span>
</td>
<td class="Name">
<span>@test.Name</span>
<input type="text" value="@test.Name" style="display:none" />
</td>
<td class="Department">
<span>@test.Department</span>
<input type="text" value="@test.Department" style="display:none" />
</td>
<td class="CreatedDate">
<span>@test.CreatedDate</span>
<input type="text" value="@test.CreatedDate" style="display:none" />
</td>
<td>
<a class="Edit" href="javascript:;">Edit</a>
<a class="Update" href="javascript:;" style="display:none">Update</a>
<a class="Cancel" href="javascript:;" style="display:none">Cancel</a>
<a class="Delete" href="javascript:;">Delete</a>
</td>
</tr>
}
</table>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="width:150px">
Name<br />
<input type="text" id="txtname" style="width:140px"/>
</td>
<td style="width:150px">
Department<br />
<input type="text" id="txtdepartment" style="width:140px" />
</td>
<td style="width:150px">
CreatedDate<br />
<input type="text" id="txtCreatedDate" style="width:140px" />
</td>
<td style="width:200px">
<br />
<input type="button" id="btnAdd" value="Add" />
</td>
</tr>
</table>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
<script type="text/javascript">
$(function () {
//Remove the dummy row if data present.
if ($("#tbltest tr").length > 2) {
$("#tbltest tr:eq(1)").remove();
} else {
var row = $("#tbltest tr:last-child");
row.find(".Edit").hide();
row.find(".Delete").hide();
row.find("span").html(' ');
}
});
// Bind data row
function AppendRow(row, Id, Name, Department, CreatedDate) {
//Bind Id.
$(".Id", row).find("span").html(Id);
//Bind Name.
$(".Name", row).find("span").html(Name);
$(".Name", row).find("input").val(Name);
// Bind Department
$(".Department", row).find("span").html(Department);
$(".Department", row).find("input").val(Department);
row.find(".Edit").show();
row.find(".Delete").show();
$("#tbltest").append(row);
};
//Add event handler.
$("body").on("click", "#btnAdd", function () {
var txtname = $("#txtname");
var txtdept = $("#txtdepartment");
var txtdate = $("#txtCreatedDate");
$.ajax({
type: "POST",
url: "/Test/InsertTest",
data: '{Name:"' + txtname.val() + '",Department:"' + txtdept.val() + '",CreatedDate:"' + txtdate.val() + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
var row = $("#tbltest tr:last-child");
if ($("#tbltest tr:last-child span").eq(0).html() != " ") {
row = row.clone();
}
AppendRow(row, r.Id, r.Name, r.Department, r.CreatedDate);
txtname.val("");
txtdept.val("");
txtdate.val("");
}
});
});
//Edit event handler.
$("body").on("click", "#tbltest .Edit", function () {
var row = $(this).closest("tr");
$("td", row).each(function () {
if ($(this).find("input").length > 0) {
$(this).find("input").show();
$(this).find("span").hide();
}
});
row.find(".Update").show();
row.find(".Cancel").show();
row.find(".Delete").hide();
$(this).hide();
});
//Update event handler.
$("body").on("click", "#tbltest .Update", function () {
var row = $(this).closest("tr");
$("td", row).each(function () {
if ($(this).find("input").length > 0) {
var span = $(this).find("span");
var input = $(this).find("input");
span.html(input.val());
span.show();
input.hide();
}
});
row.find(".Edit").show();
row.find(".Delete").show();
row.find(".Cancel").hide();
$(this).hide();
var test = {};
test.Id = row.find(".Id").find("span").html();
test.Name = row.find(".Name").find("span").html();
test.Department = row.find(".Department").find("span").html();
test.CreatedDate = row.find(".CreatedDate").find("span").html();
$.ajax({
type: "POST",
url: "/Test/UpdateTest",
data: '{test:' + JSON.stringify(test) + '}',
contentType: "application/json;charset=utf-8",
dataType: "json"
});
});
//Cancel event handler.
$("body").on("click", "#tbltest .Cancel", function () {
var row = $(this).closest("tr");
$("td", row).each(function () {
if ($(this).find("input").length > 0) {
var span = $(this).find("span");
var input = $(this).find("input");
input.val(span.html());
span.show();
input.hide();
}
});
row.find(".Edit").show();
row.find(".Delete").show();
row.find(".Update").hide();
$(this).hide();
});
//Delete event handler.
$("body").on("click", "#tbltest .Delete", function () {
if (confirm("Do you want to delete this row?")) {
var row = $(this).closest("tr");
var Id = row.find("span").html();
$.ajax({
type: "POST",
url: "/Test/DeleteTest",
data: '{Id: ' + Id + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
if ($("#tbltest tr").length > 2) {
row.remove();
} else {
row.find(".Edit").hide();
row.find(".Delete").hide();
row.find("span").html(' ');
AppendRow(row, r.Id, r.Name, r.Department, r.CreatedDate);
}
}
});
}
});
</script>
</body>
</html>
Output:
Add View
1. In this article explain to MVC crud operation in gridview list using entity framework with ajax jquery.
2. this relationship between Model,Controller and View.