Gridview行删除

时间:2010-11-29 04:58:58

标签: asp.net-mvc-2 saas

我正在使用Gridview。我在行的最后一列放了一个删除按钮。每当我单击删除按钮时,应从网格中删除特定行,也从数据库中删除。 这是我的aspx代码

<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);
}

这是我的控制器

 public ActionResult DeactivateRole(string roleId)
    {
       // What should i do here for Row Delete 
       return View();
    }

1 个答案:

答案 0 :(得分:0)

当我使用AJAX执行类似操作时,我会做两件事:

  1. 在数据库中删除
  2. 在回调时使用javascript删除行,例如

    函数DeleteEmployeeDetails(roleId)  {     if(确认('您确定要删除此角色吗?')){         $ .get(“DeactivateRole?roleId =”+ roleId,function(){

           //roleList_callBack();
    
    
    
       $('#row' + roleId).remove(); //Grab the row and delete it
    
    
       //If you don't have specific Id's for every row but your button does you could use the $.parent() function.
    
    
       $('#button'+roleId).parent().parent().remove(); //This finds the parent of the parent of the button and removes it. You can use as many parents as you'd like until you get a hold on the actual row, though this isn't always recommended due to if you change the design in some way your parent might not be correct. 
    
    
    });
    
    }

    }

  3. 如果删除在数据库中成功,则该行会立即消失,您无需重新加载数据库中的所有数据。

    如何删除数据库中的行取决于您使用的框架。如果您使用ADO.NET Entity Framework,您可以执行以下操作:

    public ActionResult DeactivateRole(string roleId)     {

       $('#row' + roleId).remove(); //Grab the row and delete it
    
    
       //If you don't have specific Id's for every row but your button does you could use the $.parent() function.
    
    
       $('#button'+roleId).parent().parent().remove(); //This finds the parent of the parent of the button and removes it. You can use as many parents as you'd like until you get a hold on the actual row, though this isn't always recommended due to if you change the design in some way your parent might not be correct. 
    
    
    });
    

    DeleteObject()方法将实际的database-entity-object作为参数,用于删除数据库中的相应条目。

    我使用的参数(dc.Roles.FirstOrDefault(r =&gt; r.ID == roleId))从数据库Roles-table中获取对象。