4

I tried to pass Array of javascript classes in $.post function, but in controller function, each member of list(argument) has default values.

JS code(View).

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
    });
}

Server code(model) :

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

Controller function

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");
   }
}

How I can pass Array of javascript class into POST request and success retrieve it in controller ?

Kronos
  • 482
  • 4
  • 19

1 Answers1

3

How to post an array of complex objects with JSON, jQuery to ASP.NET MVC Controller?

May be this will help

Community
  • 1
  • 1
Jayantha Lal Sirisena
  • 21,216
  • 11
  • 71
  • 92