Passing A List Of Objects Into An MVC Controller Method Using jQuery Ajax

前端 未结 13 1792
逝去的感伤
逝去的感伤 2020-11-22 14:22

I\'m trying to pass an array of objects into an MVC controller method using jQuery\'s ajax() function. When I get into the PassThing() C# controller method, the argument \"t

13条回答
  •  孤独总比滥情好
    2020-11-22 14:43

    This is working code for your query,you can use it.

    Controler

        [HttpPost]
        public ActionResult save(List listObject)
        {
        //operation return
        Json(new { istObject }, JsonRequestBehavior.AllowGet); }
        }
    

    javascript

      $("#btnSubmit").click(function () {
        var myColumnDefs = [];
        $('input[type=checkbox]').each(function () {
            if (this.checked) {
                myColumnDefs.push({ 'Status': true, 'ID': $(this).data('id') })
            } else {
                myColumnDefs.push({ 'Status': false, 'ID': $(this).data('id') })
            }
        });
       var data1 = { 'listObject': myColumnDefs};
       var data = JSON.stringify(data1)
       $.ajax({
       type: 'post',
       url: '/Controller/action',
       data:data ,
       contentType: 'application/json; charset=utf-8',
       success: function (response) {
        //do your actions
       },
       error: function (response) {
        alert("error occured");
       }
       });
    

提交回复
热议问题