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

后端 未结 3 422
伪装坚强ぢ
伪装坚强ぢ 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:30

    If the id of your form is frm, then it should be referenced as follows

    data:   $("#frm").serialize(),
    
    0 讨论(0)
  • 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);
       }
     });
    
    0 讨论(0)
  • 2021-01-17 05:41

    You have forgot to add id selecter:

    $.ajax(
          {
              url: '@Url.Action("CreateProduct","ProductManagement")',
              dataType: 'json',
              contentType: 'application/json; charset=utf-8',
              type: 'post',
              data:   $('#frm').serialize(),                      // $('#frm');
              success: function () { alert('s'); },
              error: function (e1, e2, e3) { alert(e2); alert(e1 + ' ' + e2 + ' ' + e3); alert('failure event'); }
          }
        );
    
    0 讨论(0)
提交回复
热议问题