Skip to content
Loading
    
  •             class="modal-footer">    
  •                 "button" class="btn btn-secondary" data-dismiss="modal">Cancel    
  •                 "submit" class="btn btn-danger">Delete    
  •                 
  •             
  •        
  •   
  •      
  • +2
  • Sachin Singh
    +2
    your syntax is incorrect in this line $('#deleteForm').attr('action', '/Location/Delete')+Id;
    follow below code 
     
    1. $('body').on('click', '[id*=btnDelete]', function (e) {  
    2. e.preventDefault();  
    3. var data = $(this).parents('tr').find('td');  
    4. Id = data.eq(0).html();  
    5. alert(Id);  
    6. $.get("/Location/Delete", function (data) {  
    7. $("#AnyDiv").html(data);  
    8. $('#deleteForm').attr('action', '/Location/Delete?Id'+Id);  
    9. $("#deleteModal").modal('show')  
    10. })  
    11. })  
     or you can also set the id in a hidden field before opening the modal
    1. $("#AnyDiv").html(data);    
    2. $('#deleteForm').attr('action', '/Location/Delete?Id'+Id);    
    3. $("#yourHiddenFieldId").val(Id);  
    4. $("#deleteModal").modal('show')    
    +2
  • +2
    Hi Sachin
     
      In Controller value in Id is null
     
    $('body').on('click', '[id*=btnDelete]', function (e) {
    e.preventDefault();
    var data = $(this).parents('tr').find('td');
    Id = data.eq(0).html();
    $.get("/Location/Delete", function (data) {
    $("#AnyDiv").html(data);
    $('#deleteForm').attr('action', '/Location/Delete?Id' + Id);
    $("#deleteModal").modal('show')
    })
    })
     
    ****
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Delete(string Id)
    {
    try
    {
    int result = dbLocation.Delete(Id);
    if (result == 1)
    {
    TempData["Message"] = "Successfully Deleted.";
    }
    else
    {
    TempData["Message"] = "Error Encountered.";
    }
    }
    catch (Exception ex)
    {
    ExceptionLogging.SendExcepToDB(ex);
    TempData["Message"] = "Error Encountered.";
    }
    return RedirectToAction("Index");
    }
     
     
    Thanks 
    +2
  • Sachin Singh
    +2
    in your code the equals to sign is missing, paste below code and it will work
    1. $('body').on('click', '[id*=btnDelete]', function (e) {  
    2. e.preventDefault();  
    3. var data = $(this).parents('tr').find('td');  
    4. Id = data.eq(0).html();  
    5. $.get("/Location/Delete", function (data) {  
    6. $("#AnyDiv").html(data);  
    7. $('#deleteForm').attr('action', '/Location/Delete?Id=' + Id);  
    8. $("#deleteModal").modal('show')  
    9. })  
    10. })  
     
    +2
  • +2
    Hi Sachin
     
      It shows ID value to right aligned . I am using this in controller but it is not working. How i should Trim the spaces
     
    Id = Regex.Replace(Id, @"\t|\n|\r", "");
     
    Thanks 
    +2
  • +1
    Hi Sachin
     
      Nothing is happening on click of btnDelete
    Delete
     
    ***** JS 
    $('body').on('click', '[id*=btnDelete]', function (e) {
    e.preventDefault();
    alert("hello");
    $.get("/Location/Delete", function (data) {
    $("#AnyDiv").html(data);
    $('#deleteForm').attr('action', '/Location/Delete');
    $("#deleteModal").modal('show')
    })
    })
     
    ***   Modal

    Are you sure you want to delete this record ?

     
    **   Controller
    [HttpGet]
    public ActionResult Delete()
    {
    return PartialView("_DeletePartial");
    }
     
    Thanks 
    +1
  • +1

    Hi

    Now when i have put href="#" in anchor tag then it has gone in Js function. Modal popup has not opened yet

     

     

    Thanks 

    +1
  • +1

    Hi Sachin

    AnyDiv was not defined . MOdal Poup is opening but when i click on Delete i gets below error

    The required anti-forgery cookie "__RequestVerificationToken" is not present.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Web.Mvc.HttpAntiForgeryException: The required anti-forgery cookie "__RequestVerificationToken" is not present.

    In place of form tag can't we use @using (Html.BeginForm).

    Thanks

    +1
  • +1
    Hi Sachin
     
       Popup also not open & get this error -
    Failed to load resource: the server responded with a status of 500 (Internal Server Error)
     
    Alert message gets printed 
     
    $('body').on('click', '[id*=btnDelete]', function (e) {
    e.preventDefault();
    var data = $(this).parents('tr').find('td');
    Id = data.eq(0).html();
    alert(Id);
    $.get("/Location/Delete", function (data) {
    $("#AnyDiv").html(data);
    $('#deleteForm').attr('action', '/Location/Delete')+Id;
    $("#deleteModal").modal('show')
    })
    })
    ************* Controller
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Delete(string Id)
    {
    try
    {
    int result = dbLocation.Delete(Id);
    if (result == 1)
    {
    TempData["Message"] = "Successfully Deleted.";
    }
    else
    {
    TempData["Message"] = "Error Encountered.";
    }
    }
    catch (Exception ex)
    {
    ExceptionLogging.SendExcepToDB(ex);
    TempData["Message"] = "Error Encountered.";
    }
    return RedirectToAction("Index");
    }
     
    Thanks 
    +1