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.

Sign up to request clarification or add additional context in comments.

1 Comment

Great answer, only this small thing and is okay :) many thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.