Skip to content
Loading
How to open Partial View in Modal Popup on click of button.    
  •             
  •        
  •   
  •      
  • +2
  • 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
  • Sachin Singh
    anchor tag has a specific purpose, why don't you simply use the button.
    1. "btnDelete" type="button" class="btn btn-danger btn-sm" style='margin-left:5px'>class='fa fa-trash'> Delete   
     
    1. $("#btnDelete").click(function(){      

    2. $.get("/Common/getDeleteModal",function(data){    
    3. $("#AnyDiv").html(data);    
    4. $('#deleteForm').attr('action''/Location/Delete');    
    5. $("#deleteModal").modal('show')    
    6. })    
    7.       
    8. })      
    +2
  • 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
  • Sachin Singh
    after the modal opens and you click delete, there must be an Id of the currently selected row that should go to the controller's Delete Action, where you have written the logic for delete.
    +2
  • 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
  • Sachin Singh
    during delete click
    1. $("#btnDelete").click(function(e){    
    2. e.preventDefault();    
    3.     
    4. $.get("/Common/getDeleteModal",function(data){  
    5. $("#AnyDiv").html(data);  
    6. $('#deleteForm').attr('action''/Location/Delete');  
    7. $("#deleteModal").modal('show')  
    8. })  
    9.     
    10. })    
     
    +2
  • Asma Khalid
    Look into this https://bit.ly/2Ze0FUe
     
    Mark answer as accepted if helpful
     
    Thanks & Regards,
    Asma Khalid
     
    For More Visit: www.asmak9.com or www.bytezaar.com
    +3
  • Sachin Singh

    step 1. Change the partial view as

    create a common controller for common functionalities and return the partial view

    public ActionResult getDeleteModal()  
    {
        return PartialView("_DeletePartial");  
    }

    step 3. Call it using ajax and change the action attribute of form as

    $.get("/Common/getDeleteModal",function(){  
    $('#deleteForm').attr('action', '/Location/Delete'); 
    $("#deleteModal").modal('show')  
    })
    +3