I am having a Gridview. I had placed a delete button in the last column of the row. whenever i click the delete button, the particular row should be deleted from the grid and also from the database.
Here is my aspx code
<script language="javascript" type="text/javascript">
function DeleteEmployeeDetails(roleId)
{
if (confirm('Are you sure you want to delete this role?'))
{
$.get("DeactivateRole?roleId=" + roleId,
function ()
{
$('#row' + roleId).remove();
//or
$('#button' + roleId).parent().parent().remove();
});
}
} function loadRoleList()
{
$.ajaxSettings.cache = false;
$.get(roleListActionUrl, roleList_callBack);
}
//This method is used to render the role details in the role List div
function roleList_callBack(data) {
data = data + '<div id="AddRole"></div>';
$("#EmpDetails").html(data);
}
This is my controller
public ActionResult DeactivateRole(string roleId)
{
// What should i do here for Row Delete
return View();
}
When I do something similar with AJAX I do two things:
Delete the row using javascript on callback e.g.
function DeleteEmployeeDetails(roleId)
{
if (confirm(‘Are you sure you want to delete this role?’)) {
$.get(“DeactivateRole?roleId=” + roleId, function() {
If the delete succeeds in your database the row disappears instantly and you don’t have to reload all the data from the database.
How you delete a row in your database depends on which framework you use. If you use ADO.NET Entity Framework you could do something like this:
public ActionResult DeactivateRole(string roleId)
{
The DeleteObject() method takes an actual database-entity-object as argument which it uses to delete the corresponding entry in you database.
The argument I use(dc.Roles.FirstOrDefault(r => r.ID == roleId)) gets the object from the database Roles-table.