c# - How to pass parameter via ajax mvc? -


i have problem follow : click button delete , show modal popup , in modal, have 1 button "ok", when click button "ok" , process event click of button "ok", such call action delete via ajax. code snippet.

      @foreach (var item in model)                 {                     <tr>                         <td>                             @ajax.actionlink(" delete", "", "", new { id = @item.userprofileid }, new ajaxoptions()                             {                                 httpmethod = "get",                                 insertionmode = insertionmode.insertafter,                                 updatetargetid = "",                                 onsuccess = "showmodal()"                             }, new { @class = "fa fa-times btn btn-danger btn-xs", @id = "bt_del_profile" })                         </td>                     </tr>                 }  <div id="mymodal" aria-hidden="true" class="modal fade">     <div class="modal-dialog">         <div class="modal-content">             <div class="modal-footer">                 <button type="button" id="bt_del_ok" class="btn btn-success btn-sm">ok</button>             </div>         </div>     </div> </div>  <script type="text/javascript">      function showmodal() {         $("#mymodal").modal('show');     }      $('#bt_del_ok').click(function (e) {         e.preventdefault();         $.ajax({             url: '/profile/delprofile',             type: 'post',             contenttype: 'application/json; charset=utf-8',             datatype: 'json',             cache: false,             //processdata: false,             data: { id: @item.userprofileid },                 success: function (result) {                     alert("ok");                 },              error: function (result) {                  alert("error delete");              }          });     });  </script> 

the problem in here data: { id: @item.userprofileid }, . can not id in here although when show devtool chrome , see <a class="fa fa-times btn btn-danger btn-xs" data-ajax="true" data-ajax-method="get" data-ajax-success="showmodal()" href="/home/index/p000000002" id="bt_del_profile"> delete</a> can tell me this? , give me advices. thank you.

even though using @item.userprofileid within java script, it's asp.net. if had value coming javascript, use data: { id: '12345' },

which means code should data: { id: '@item.userprofileid' }. should fix problem.

some 'ideas'

you can change show modal this.

<div aria-hidden="true" class="modal fade mymodal"> <div class="modal-dialog">     <div class="modal-content">         <div class="modal-footer">             <button type="button" id="bt_del_ok" class="btn btn-success btn-sm">ok</button>         </div>     </div> </div> 

then change showmodal below.

function showmodal(userprofileid) {     $(".mymodal").prop('id', userprofileid);     $(".mymodal").modal('show'); } 

then in delete event;

 var userprofileid =  $(".mymodal").prop('id');  data: { id:userprofileid }, 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -