如何将Javascript数组对象发送到MVC5控制器方法

时间:2015-02-12 09:03:56

标签: ajax model-view-controller

嗨,下面是我的问题。 如何将Javascript数组对象发送到MVC5控制器方法。

代码成功点击了action方法,但列表值param值为null。我尝试了与JSON.stringify的不同组合。

    //object class
    public class  MassPayoutItem
        {
           public string ReciverEmailID { get; set; }
           public string Amount { get; set; }
           public int ProviderID { get; set;}
           public string AppintmentsID { get; set; }
           public string TransictionID{get;set;}
           public string TransictionStatus { get; set; }
           public string ProviderName { get; set;}
        }

    //Action method
     public ActionResult GetProviderMassPaymentDetail(List<MassPayoutItem> PayoutItemList){
      return Json(new { Result = true, ResultData = ResaultData, Message = "" }, JsonRequestBehavior.AllowGet);

    }


    //JavaScript Code
    function MassPay() {
                alert("called MassPay");
                var MassPymentList = new Array();
                var objs;
                $('.Include_Payment:checked').each(function () {

                    var ReciverEmailId = $(this).parents('tr').attr("emailid");
                    var appointmentids = $(this).parents('tr').attr("appointmentids");
                    var ProviderID = $(this).parents('tr').attr("UserId");
                    var Amount = $(this).parents('tr').find(".Amount").text();
                    var ProviderName = $(this).parents('tr').find(".OwnerName").text();
                    MassPymentList.push({ "ReciverEmailID": ReciverEmailId, "Amount": Amount, "ProviderID": ProviderID, "AppintmentsID": appointmentids, "ProviderName": ProviderName, "TransictionID": "abc", "TransictionStatus": "bcd" });
                });
                objs = JSON.stringify({ "PayoutItemList": MassPymentList });
                         debugger;

// _PageUrl.PayMassTransiction 
// '@Url.Action("GetProviderMassPaymentDetail","Controller")'            
//The call hits the method but the value is null
                $.ajax({
                    Type: "POST"
                    , url: _PageUrl.PayMassTransiction
                    , contentType: "application/json,charset=utf-8",
                    traditional: true
                   , data: objs,
                    datatype: "json",
                    success: function (result) {
                        debugger;
                        alert("called");
                    }
                    , error: function (result) {

                    }
                });

            }

1 个答案:

答案 0 :(得分:0)

嗨,最后我解决了这个问题。

 public class  MassPayoutItem
        {
           public string ReciverEmailID { get; set; }
           public string Amount { get; set; }
           public int ProviderID { get; set;}
           public string AppintmentsID { get; set; }
           public string TransictionID{get;set;}
           public string TransictionStatus { get; set; }
           public string ProviderName { get; set;}
        }

    //Action method
    **//This is first change i have make.**
      [HttpPost, ActionName("GetProviderMassPaymentDetail")]
     public ActionResult GetProviderMassPaymentDetail(List<MassPayoutItem> PayoutItemList){
      return Json(new { Result = true, ResultData = ResaultData, Message = "" }, JsonRequestBehavior.AllowGet);

    }


    //JavaScript Code
    function MassPay() {
                alert("called MassPay");
                var MassPymentList = new Array();
                var objs;
                $('.Include_Payment:checked').each(function () {

                    var ReciverEmailId = $(this).parents('tr').attr("emailid");
                    var appointmentids = $(this).parents('tr').attr("appointmentids");
                    var ProviderID = $(this).parents('tr').attr("UserId");
                    var Amount = $(this).parents('tr').find(".Amount").text();
                    var ProviderName = $(this).parents('tr').find(".OwnerName").text();
                    MassPymentList.push({ "ReciverEmailID": ReciverEmailId, "Amount": Amount, "ProviderID": ProviderID, "AppintmentsID": appointmentids, "ProviderName": ProviderName, "TransictionID": "abc", "TransictionStatus": "bcd" });
                });
                objs = JSON.stringify({PayoutItemList: MassPymentList });
                         debugger;

// _PageUrl.PayMassTransiction 
// '@Url.Action("GetProviderMassPaymentDetail","Controller")'            
//The call hits the method but the value is null
// below is the changed ajax calll change Type to type               
 $.ajax({
                type: "POST"
                , url: _PageUrl.PayMassTransiction
                , contentType: "application/json,charset=utf-8"
                ,async: false
            ,traditional: true
               , data: objs,
                datatype: "json",
                success: function (result) {
                    debugger;
                    alert("called");
                }
                , error: function (result) {

                }
            });
            }