0

i've tried to send a class with two list with ajax to an mvc controler.

MVC Controler :

[HttpPost]
public JsonResult AfficherDetailsFacture(AfficheDetails afficheDetailsFacture)
{ 
   // do some stuff
}

C# Class for the controler argument:

[Serializable]
public class AfficheDetails
{

    public List<long> firstList{ get; set; }

    public List<long> secondList{ get; set; }

    public string numFacture{ get; set; }
}

JS :

var AfficheDetails = {
    firstList: [31025,6954], 
    secondList:  [31542,31211,23214,23211],
    numFacture: "Facture 001"
};    

$.ajax({
    type: "POST",
    async: false,
    url: urls.urlAfficherDetailsFacture,
    contentType: 'application/json',
    data: JSON.stringify(AfficheDetails),
    success: function (oResultat) {
      //
    }

This is NOT working properly.

If firstlist has less element than secondList, then firstList is null in the MVC controller.

i've been working several hours to discover this "rule". If firstList has more element or the same ammount, it is working !

Is it a MVC Binding bug ?

Jean-Baptiste HENRY
  • 2,169
  • 2
  • 12
  • 4
  • take a look on this [answer](http://stackoverflow.com/questions/12572856/jquery-post-array-asp-net-mvc-4) – Sergey Aug 01 '14 at 11:55
  • @SergeyBoiko : this answer is with a single list, in this case it is not working all the time, but it is working some time. And i've the contentType arg. – Jean-Baptiste HENRY Aug 01 '14 at 12:00
  • That seems like a very strange "rule". Have you tried taking a look at what is actually sent across the wire (using Fiddler or a debugger in your browser)? Does removing the secondList property from the model make a difference? – Rune Aug 01 '14 at 12:14
  • @Rune i've check the data send with firebug, it is always good. If i remove the second list, it working all the time. – Jean-Baptiste HENRY Aug 01 '14 at 12:24
  • @Jean-BaptisteHENRY don't know what is the issue,try with the AfficheDetails.serializeArray() instead of JSON.Stringify . – Arun Manjhi Aug 01 '14 at 13:18

1 Answers1

1

You can also use on Controller

public JsonResult AfficherDetailsFacture(string jsonData)
{ 
 var jss = new JavaScriptSerializer();
 var data = jss.Deserialize<AfficheDetails>(jsonData);
   // do some stuff
}

where data now contains all the values of the class AfficheDetails

and on Jquery

edit the line

$.ajax({
    type: "POST",
    async: false,
    url: urls.urlAfficherDetailsFacture,
   // contentType: 'application/json',  comment the type
    data: { jsonData : JSON.stringify(AfficheDetails) },
    success: function (oResultat) {
      //
    }
Arun Manjhi
  • 175
  • 9