2

I have a problem with object send in json format, that is not binded in Action Controller

Here the json parts:

function filterTxAjustement(arrTx) {
    return {
        dateDebut: $("#TxAjustementDateDebut").val(),
        dateFin: $("#TxAjustementDateFin").val(),
        ufCode: $("#TxAjustementUf").val(),
        tx: JSON.stringify(arrTx)
    };
}

function SaveTx() {

    var arrTx = [];

    $.each(Day, function (i, v) {
        var $this = $('#' + v + 'Tx');
        var tx = new Object();
        tx.Day = v;
        tx.Nb = parseInt($this.val());
        tx.UfCode = parseInt($('#TxAjustementUf').val());
        tx.Total = 0;
        arrTx.push(tx);
    });

    $.ajax({
        url: EasilyRelativeUrl("Lit/SetTxAjustementByDateAndUf"),            
        data: filterTxAjustement(arrTx),
        type: 'POST',
        dataType: 'json'
    }).done(function (data) {

        $('#TxAjustementGrid').data("kendoGrid").dataSource.read();
    });
}

My array is well populated:

dateDebut:16/12/2013 dateFin:22/12/2013 ufCode:21124 tx:[{"Day":"Sunday","Nb":1,"UfCode":21124,"Total":0},{"Day":"Monday","Nb":2,"UfCode":21124,"Total":0},{"Day":"Tuesday","Nb":3,"UfCode":21124,"Total":0},{"Day":"Wednesday","Nb":0,"UfCode":21124,"Total":0},{"Day":"Thursday","Nb":0,"UfCode":21124,"Total":0},{"Day":"Friday","Nb":0,"UfCode":21124,"Total":0},{"Day":"Saturday","Nb":0,"UfCode":21124,"Total":0}]

But when it hit Action Controller, tx is null

        public ActionResult SetTxAjustementByDateAndUf(DateTime dateDebut, DateTime dateFin, string ufCode, List<TauxAjustement> tx)

(Here the TauxAjustement object)

    public class TauxAjustement
{
    public string Day { get; set; }
    public int Nb { get; set; }
    public int Total { get; set; }
    public int UfCode { get; set; }
}

I have tried with TauxAjustement[], but same issue. I have added Total = 0 and parseInt to have exact definition of C# object, but same... What have I missed ? I do make a CustomBinderModel for this ?

Thanks for your help.

User.Anonymous
  • 1,719
  • 1
  • 28
  • 51
  • 2
    Check http://stackoverflow.com/questions/18373202/asp-net-mvc-4-json-binding-to-the-view-model-nested-object-error & http://stackoverflow.com/questions/4164114/posting-json-data-to-asp-net-mvc – Danila Polevshchikov Dec 16 '13 at 11:23
  • 1
    That's works now I've removed JSON.stringify for the array as say in the 1st link. Thank you :) – User.Anonymous Dec 16 '13 at 11:30

1 Answers1

0

You need to define the tx argument as an array rather than a List:

public ActionResult SetTxAjustementByDateAndUf(DateTime dateDebut, 
                                               DateTime dateFin, 
                                               string ufCode, 
                                               TauxAjustement[] tx)
{}
Paul Taylor
  • 5,651
  • 5
  • 44
  • 68