2012-08-23 42 views
1

我想在我的控制器中使用ajax和传递一个对象来调用一个方法。当涉及到变量时,我可以很好地做到这一点,但似乎无法使用对象。该方法被称为罚款,但对象值始终为空。我也尝试使用.toJSON方法,但得到此错误未捕获TypeError:对象函数(a,b){返回新的d.fn.init(a,b,g)}没有方法'toJSON',是的,我有JSON2包括发送JSON对象到我的Ajax方法在一个controlelr

这是迄今为止我尝试:

var VoucherDetails = this.GetVoucherDetails(); 

    $.post("/Vouchers/GetVoucherPreviewTemplate", { "Voucher": VoucherDetails}, 
      function (data) { 


      }); 


    function GetVoucherDetails() 
    { 
     var teet = $("#Title").val();  


     return { Title: teet }; 
    } 


C# 

    [HttpPost] 
     public ActionResult GetVoucherPreviewTemplate(ENT_Voucher Voucher) 
     { 
      return Json(""); 
     } 

这里是我的ENT_Voucher代码:

[Serializable] 
    public class ENT_Voucher : ENT_VoucherEntityBase 
    { 
     public int BusinessID { get; set; } 
     public int? SiteID { get; set; } 
     public string Title { get; set; }  
     public string Description { get; set; } 
     public string Code { get; set; } 
     public string Link { get; set; } 
     public DateTime StartDate { get; set; } 
     public DateTime ExpiryDate { get; set; } 
     public string ImageLink { get; set; } 
     public int Status { get; set; } 
     public ENT_Business BusinessDetails { get; set; } 
     public string VoucherTandC { get; set; } 
     public int Type { get; set; } 
     public string DaysRemaining { get; set; } 
     public int RequestCount { get; set; } 
     public bool IsPetoba { get; set; } 

     public ENT_Voucher() 
     { 
      this.BusinessDetails = new ENT_Business(); 
     } 
    } 
+0

你还可以显示ENT_Voucher C#类吗?到目前为止你的代码看起来没问题 –

回答

2

你可以把它作为一个JSON请求,允许你发送任意复杂的对象:

var VoucherDetails = this.GetVoucherDetails(); 
$.ajax({ 
    url: '@Url.Action("GetVoucherPreviewTemplate", "Vouchers")', 
    type: 'POST', 
    contentType: 'application/json', 
    data: JSON.stringify({ voucher: VoucherDetails }), 
    success: function(result) { 

    } 
}); 
+0

哇!工作,谢谢! – Funky

0

如果您有JSON2.js,那么您希望使用JSON.stringify()并将它传递给您的AJAX调用中的序列化数据!

相关问题