asp.net mvc - 将javascript类传递给POST请求

时间:2011-03-01 09:10:38

标签: javascript asp.net-mvc

我试图在 $。post 函数中传递 Array 的javascript类,但在控制器函数中,list(参数)的每个成员都有默认值。

JS代码(查看)。

function CashCollectionModel() 
{
     this.Nominal = 0;
     this.NominalCount = 0;
     this.CriticalBalance = 0;
}
function SendRequest()
{
    var arr = new Array();

    var cash_nominal = new CashCollectionModel();

    cash_nominal.Nominal = 10;
    cash_nominal.NominalCount = 100;
    cash_nominal.CriticalBalance = 20;

    arr.push(cash_nominal);

    $.post('<%= Url.Action("EditCashCollection", "Terminals") %>', 
    {
         nominals : arr
    });
}

服务器代码(型号):

namespace TerminalsExtension.Presentation.Models
{
    public class CashCollectionModel
    {
       public long Nominal {set;get;}
       public long NominalCount {set;get;}
       public long CriticalBalance {set;get;}
    }
}

控制器功能

public partial class TerminalsController : SharedControllerBase
{
   [Authorize]
   [AcceptVerbs(HttpVerbs.Post)]
   public ActionResult EditCashCollection(List<CashCollectionModel> nominals) 
   {
          // each member of "nominals" has 0 value


          return ExtView("CashCollection");
   }
}

如何将javascript类的 Array 传递给POST请求并成功在控制器中检索它?

1 个答案:

答案 0 :(得分:3)