Skip to content
Loading
asp.net core passing jquery val as asp-route-id parameter
  • Marius Vasile
    I tried with ajax
     
    1. $(function () {  
    2.             $('#btnPost').on('click', function () {  
    3.                 debugger;  
    4.                 var id = $("#fun").val();  
    5.                 $.ajax({  
    6.                     type: 'POST',  
    7.                     url: '?handler=Test',  
    8.                     beforeSend: function (xhr) {  
    9.                         xhr.setRequestHeader("XSRF-TOKEN",  
    10.                             $('input:hidden[name="__RequestVerificationToken"]').val());  
    11.                     },  
    12.                     contentType: "application/json",  
    13.                     dataType: "json",  
    14.                     data: { id: id },  
    15.                     success: function (response) {  
    16.   
    17.                     },  
    18.                     failure: function (response) {  
    19.                         alert(response);  
    20.                     }  
    21.                 });  
    22.             })  
    23.         });    
    button
    1. class="btn form-control" type="button" id="btnPost">Button  
    fun textbox
    1. "hidden" value="" id="fun"/>  
    but I have no action whatsoever, no error, the id gets data 
    +1
  • Marius Vasile
    Thank you Kiran
     
    On button click I have only one function
     
    1. return RedirectToPage("/WO/Test2", new { id = id });   
    and the id is the one I try to send through jquery. The problem I have is that is not sent
    +1
  • Kiran Mohanty
    On button click if you are redirecting to some other page , then probably you can do as below.
     
     
    1. class="btn form-control" type="submit" asp-page-handler="Test" id="testid">Button     
    2.   
    3. $('#testid').click(function (e) {  
    4.         e.preventDefault();  
    5.         var id = $("#AssetID").val(data.assetID);   
    6.         if (id > 0) {  
    7.             window.location.href = $('#testid').attr('href') + '/' + id;  
    8.         }          
    9.     });  
     
    Please  remove asp-route attribute and make the button as "a" tag. (some styles you may need to add or remove accordingly)
    +1
  • Sachin Singh
    on change of select , i am setting the value , so it should send the value.
    +1
  • Marius Vasile
    It does not send any data. Is either blank if I use asp-route-id="" or it sends dumyData if I use asp-route-id="dumyData"
    +1
  • Sachin Singh
    I don't know this will work or not , but this should work.
     
    1. $.getJSON(`?handler=AssetDetails&id=${categoryId}`, (data) => {      
    2.             if (!jQuery.isEmptyObject(data)) {      
    3.                 $("#AssetID").val(data.assetID);      
    4.                 $("#AssetName").val(data.assetName);      
    5.            var x= data.assetID;    
    6.                $('#testId').attr('asp-route-id',x );     
    7.             }      
    8.         });       
    9.     });     
     and remove the attribute value from button, means make it null.
     
    1. class="btn form-control" type="submit" asp-page-handler="Test" asp-route-id="" id="testid">Button 
    +1