How to post the viewmodel to action method using JQUERY AJAX in MVC

后端 未结 3 440
伪装坚强ぢ
伪装坚强ぢ 2021-01-17 04:39

I want to achieve create (INSERT) screen using $.POST or $.AJAX.

Note: code is working fine without AJAX call..it is already there.. now i need to do ajax call and s

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-17 05:37

    For id Selector you have to use "#" with id.

    This will work :

    $.ajax(
      {
          url: '@Url.Action("CreateProduct","ProductManagement")',
          dataType: 'json',
          contentType: 'application/json; charset=utf-8',
          type: 'post',
          data:   $('this').serialize(), OR  $('#frm').serialize(),   <-----------------
          success: function () { alert('s'); },
          error: function (e1, e2, e3) { alert(e2); alert(e1 + ' ' + e2 + ' ' + e3); alert('failure event'); }
      }
     );
    

    OR Try this :

    var form = $('#frm');
    $.ajax({
     cache: false,
     async: true,
     dataType: 'json',
     contentType: 'application/json; charset=utf-8',
     type: "POST",
     url: form.attr('action'),
     data: form.serialize(),
     success: function (data) {
     alert(data);
       }
     });
    

提交回复
热议问题