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>
Rafał Developer
  • 2,135
  • 9
  • 40
  • 72

1 Answers1

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.

Community
  • 1
  • 1
Andrei
  • 55,890
  • 9
  • 87
  • 108