2015-02-12 37 views
0

嗨,下面是我的問題。 如何將Javascript數組對象發送到MVC5控制器方法。如何將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) { 

        } 
       }); 

      } 
+0

js對象不應該在數組中嗎? – SWa 2015-02-12 09:06:49

+0

凱爾我不明白你的觀點,你可以詳細說明一下,如果有什麼地方出了問題,請通過修改代碼來解決問題。 – Mohit 2015-02-12 09:15:36

+0

我認爲'objs = JSON.stringify(MassPymentList);'就夠了 – SWa 2015-02-12 09:29:24

回答

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

       } 
      }); 
      } 
相關問題