I have already inserted few records in the table.
The Controller consists of following Action methods.
Before returning to the View, a dummy (empty) record is added to the Generic List collection at first position (Index).
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
CustomersEntities entities = new CustomersEntities();
List<Customer> customers = entities.Customers.ToList();
//Add a Dummy Row.
customers.Insert(0, new Customer());
return View(customers.ToList());
}
[HttpPost]
public JsonResult InsertCustomer(Customer customer)
{
using (CustomersEntities entities = new CustomersEntities())
{
entities.Customers.Add(customer);
entities.SaveChanges();
}
return Json(customer);
}
[HttpPost]
public ActionResult UpdateCustomer(Customer customer)
{
using (CustomersEntities entities = new CustomersEntities())
{
Customer updatedCustomer = (from c in entities.Customers
where c.CustomerId == customer.CustomerId
select c).FirstOrDefault();
updatedCustomer.Name = customer.Name;
updatedCustomer.Country = customer.Country;
entities.SaveChanges();
}
return new EmptyResult();
}
[HttpPost]
public ActionResult DeleteCustomer(int customerId)
{
using (CustomersEntities entities = new CustomersEntities())
{
Customer customer = (from c in entities.Customers
where c.CustomerId == customerId
select c).FirstOrDefault();
entities.Customers.Remove(customer);
entities.SaveChanges();
return Json(customer);
}
}
}
Once the response is received, a new row is appended to the HTML table using the AppendRow function.
Once the response is received, the HTML SPAN elements are made visible and the TextBoxes are made hidden for the Name and Country columns of the HTML Table row.
Once the response is received the respective row is removed from the HTML Table row.
@model IEnumerable<Customer>
@{
Layout = null;
WebGrid webGrid = new WebGrid(source: Model,canPage:true,canSort:false);
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@webGrid.GetHtml(
htmlAttributes:new { @id = "WebGrid", @class = "Grid" },
columns:webGrid.Columns(
webGrid.Column(header:"Customer Id", format:@<span class="label">@item.CustomerId</span>, style:"CustomerId" ),
webGrid.Column(header:"Name", format:@<span><span class="label">@item.Name</span>
<input class="text" type="text" value="@item.Name" style="display:none" /></span>, style:"Name"),
webGrid.Column(header:"Country", format:@<span><span class="label">@item.Country</span>
<input class="text" type="text" value="@item.Country" style="display:none" /></span>, style:"Country"),
webGrid.Column(format:@<span class="link">
< 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>
</span> )))
<br />
<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">
Country:<br />
< input type="text" id=" txtCountry" style=" width:140px" />
</td>
<td style="width:100px">
<br />
< input type="button" id=" btnAdd" value="Add" />
</td>
</tr>
</table>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
var row = $("#WebGrid TBODYtr:eq(0)");
if ($("#WebGrid TBODYtr").length > 1) {
row.remove();
}else {
row.find(".label").html("");
row.find(".text").val("");
row.find(".link").hide();
}
});
function AppendRow(row,customerId, name, country) {
//Bind CustomerId.
$(".CustomerId", row).find(".label").html(customerId);
//Bind Name.
$(".Name", row).find(".label").html(name);
$(".Name", row).find(".text").val(name);
//Bind Country.
$(".Country", row).find(".label").html(country);
$(".Country", row).find(".text").val(country);
row.find(".link").show();
$("#WebGrid TBODY").append(row);
};
//Add event handler.
$("body" ).on("click" , "#btnAdd" , function () {
var txtName = $("#txtName");
var txtCountry = $("#txtCountry");
$.ajax({
type:"POST",
url:"/Home/InsertCustomer",
data:'{name: "' +txtName.val() +'", country: "' +txtCountry.val() +'" }',
contentType:"application/json; charset=utf-8",
dataType:"json",
success:function (r) {
var row = $("#WebGrid TBODYtr:last-child").clone();
if (row.find(".label").is(":empty")) {
$("#WebGrid TBODYtr:last-child").remove();
}
AppendRow(row,r.CustomerId,r.Name,r.Country);
txtName.val("");
txtCountry.val("");
}
});
});
//Edit event handler.
$("body" ).on("click" , "#WebGrid TBODY .Edit" , function () {
var row = $(this).closest("tr");
$("td", row).each(function () {
if ($(this).find(".text").length > 0) {
$(this).find(".text").show();
$(this).find(".label").hide();
}
});
row.find(".Update").show();
row.find(".Cancel").show();
row.find(".Delete").hide();
$(this).hide();
});
//Update event handler.
$("body" ).on("click" , "#WebGrid TBODY .Update" , function () {
var row = $(this).closest("tr");
$("td", row).each(function () {
if ($(this).find(".text").length > 0) {
var span = $(this).find(".label");
var input = $(this).find(".text");
span.html(input.val());
span.show();
input.hide();
}
});
row.find(".Edit").show();
row.find(".Delete").show();
row.find(".Cancel").hide();
$(this).hide();
var customer = {};
customer.CustomerId = row.find(".CustomerId").find(".label").html();
customer.Name = row.find(".Name").find(".label").html();
customer.Country = row.find(".Country").find(".label").html();
$.ajax({
type:"POST",
url:"/Home/UpdateCustomer",
data:'{customer:' +JSON.stringify(customer) +'}',
contentType:"application/json; charset=utf-8",
dataType:"json"
});
});
//Cancel event handler.
$("body" ).on("click" , "#WebGrid TBODY .Cancel" , function () {
var row = $(this).closest("tr");
$("td", row).each(function () {
if ($(this).find(".text").length > 0) {
var span = $(this).find(".label");
var input = $(this).find(".text");
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" , "#WebGrid TBODY .Delete" , function () {
if (confirm("Do you want to delete this row?")) {
var row = $(this).closest("tr");
var customerId = row.find(".label").html();
$.ajax({
type:"POST",
url:"/Home/DeleteCustomer",
data:'{customerId: ' +customerId +'}',
contentType:"application/json; charset=utf-8",
dataType:"json",
success:function (response) {
if ($("#WebGrid TBODYtr").length == 1) {
row.find(".label").html("");
row.find(".text").val("");
row.find(".link").hide();
}else {
row.remove();
}
}
});
}
});
</script>
</body>
</html>