Skip to content
Loading
  
  •                                                         class="modal-footer">  
  •                                                             "button" class="btn btn-secondary" data-dismiss="modal">Cancel  
  •                                                             "submit" class="btn btn-danger">Delete  
  •                                                           
  •                                                       
  •                                                   
  •                                               
  •                                         }  
  •  Note:- I am using ViewBag.Id and Modal Id is simple DeleteModal
     
    step 2.  Remove the data toggle from anchor tag and use data-Id
    1. @if (item.IsActive)  
    2.                                        {  
    3.                                            btnDelete class="btn btn-danger  btn-sm btnDelete" data-Id="@item.Id" style='margin-left:5px'>class='fa fa-trash'> Delete   
    4.                                        }  
    5.                                        else  
    6.                                        {  
    7.                                            class='btn btn-danger btn-sm disabled' id='btnDelete' style='margin-left:5px'>class='fa fa-trash'> Delete   
    8.                                        }  
     
     step 3. create one method to return the above partial view.
    1. public ActionResult DeletePartial(int Id)  
    2. {  
    3. ViewBag.Id= Id;  
    4. return PartialView("_DeletePartial");  
    5.   
    6. }  
    step 4. On delete click first call the partial view then open the modal popup.
     Note:- myDiv in the below code is just a blank div anywhere in the view.
     
    1. $(".btnDelete").click(function(){  
    2. int Id= $(this).attr("data-Id");  
    3. $.Post("/Controller/DeletePartial?Id="+Id,function(data){  
    4. $("#myDiv").html(data);  
    5.   
    6. })  
    7. $("#DeleteModel").Modal('Open');  
    8. });  
    +2
  • Sachin Singh
    +1
    partial view inside foreach loop doesn't make sense. I will say it is wrong.
    you should call the partial view on delete click using ajax.
    +1
  • +1

    Hi Sachin

     

       Can u pls Help with Code. 

     

    Thanks 

    +1
  • +1
    Hi Arnab
     
       Is it possible to use the code for all Delete in different controllers.
    don't want to write for each Controller Delete method
     
    Thanks 
    +1
  • Sachin Singh
    +1
    Why do you need to create this method in every controller ? just pass the same controller and action name from every view along with the id.
    +1
  • +1
    Hi Sachin
     
        Table will be different . IN below example it is Customer . IN other case table may be different
     
    public ActionResult DeleteCustomer(string Id)
    {
    int result = dbCustomer.Delete(Id);
    if (result == 1)
    {
    TempData["Message"] = "Successfully Deleted.";
    }
    else
    {
    TempData["Message"] = "Something went wrong.";
    }
    return RedirectToAction("Index");
    }
     
    +1