2

Here I have got problem:

OneCar -> when I take only one argument I can see value and everything is OK

but when I try take list of arguments in ListCar1 and ListCar2 I can see only null

I believe this is small mistake but I don't know how to fix it.

[HttpPost]
        public JsonResult DodajTematSave(string OneCar, string[] ListCar1, List<string> ListCar2)
        {
        }

Here is Json action

<script type="text/javascript">

    function Save() {

        var mycars = new Array()
        $("[name^='CarString']").each(function () {
            mycars.push(this.value);
        });

        $.ajax({
            url: '@Url.Action("DodajTematSave", "StronaGlowna")',
            dataType: "json",
            data: {
                OneCar: mycars[0]
                ListCar1: mycars
                ListCar2: mycars
            },
            type: "POST",
            async: false,
            error: function () {
            },
            success: function (data) {
                if (data.Success) {
                    alert('success');
                }

            }
        });
    }

</script>

Correct answer:

<script type="text/javascript">

    function Save() {

        var mycars = new Array()
        $("[name^='CarString']").each(function () {
            mycars.push(this.value);
        });

        $.ajax({
            url: '@Url.Action("DodajTematSave", "StronaGlowna")',
            dataType: "json",
            data: {
                OneCar: mycars[0]
                ListCar1: mycars
                ListCar2: mycars
            },
            type: "POST",
            traditional: true,
            async: false,
            error: function () {
            },
            success: function (data) {
                if (data.Success) {
                    alert('success');
                }

            }
        });
    }

</script>

1 Answer 1

2

You need to add the traditional: true parameter to the $.ajax call. Details can be found in this jQuery forum thread or in this answer.

1
  • Great answer, only this small thing and is okay :) many thanks :) Commented Mar 25, 2013 at 12:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.