【问题标题】:How to send Javascript array object to the MVC5 controller method如何将 Javascript 数组对象发送到 MVC5 控制器方法
【发布时间】:2015-02-12 09:03:56
【问题描述】:

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

代码成功命中动作方法,但列表值参数值为空。我也尝试了与 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) {

                    }
                });

            }

【问题讨论】:

  • js对象不应该是数组吗?
  • Kyle 我不明白你的意思,如果那里有问题,你可以通过修改代码来详细说明你的答案。
  • 我认为objs = JSON.stringify(MassPymentList); 就足够了
  • 我试试 objs=JSON.stringify(MassPymentList);它发送空值。
  • 您是否允许对控制器操作进行 POST 操作?

标签: ajax model-view-controller


【解决方案1】:

嗨,我终于解决了这个问题。

 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) {

                }
            });
            }

【讨论】:

    猜你喜欢
    • 2013-07-24
    • 2013-09-26
    • 2021-03-17
    • 2019-06-21
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多