1

I have this code in razor

@{
    var aid = new List<int>();
    foreach (var item in Model.applications)
    {
        if (item.Status == 1)
        {
            aid.Add(item.Id);
        }
    }
}

and in js I have this:

<script>
function checkIfSigned() {

    var data = { aid: @aid };
    console.log(data);

    $.ajax({
        url: '@Url.Action("Method", "Controller")',
        type: "POST",
        data: data,
        ...
    });
}

and in my controller:

public JsonResult Method(List<int> aid)
{
  foreach (var item in aid)
  { ... }
}

my problem is:

var data = { aid: @aid };

It throws an exception:

Uncaught SyntaxError: Unterminated template literal

var data = { aid: System.Collections.Generic.List`1[System.Int32] };

What can I do? how can I post list?

7
  • The string representation of an object is just the class name of that object. You can perhaps JSON-serialize the object when outputting it to the page. Though why do you need to do this in the first place? You're sending unmodified data back to the server which just gave you that data. Seems unnecessary. Commented Apr 5, 2017 at 13:02
  • You need to convert your c# list to a javascript array. But why is that code in your view (it belongs in your controller) Commented Apr 5, 2017 at 13:03
  • I know it should be in controller but for now I haven't any other way.. I must check these values using timeout Commented Apr 5, 2017 at 13:11
  • 2
    Sorry, but what your doing makes not sense (and as @David has noted, its pointless) - but your can use var data = { aid: @Html.Raw(Json.Encode(aid)) }; and the you need data: JSON.stringify(data); and contentType: 'application/json', Commented Apr 5, 2017 at 13:17
  • @StephenMuecke thanks. and what type of parameter should I pass in action method? Commented Apr 5, 2017 at 13:28

1 Answer 1

1

You can do like that :

var data = []; // javascript array
@foreach(var item in Model.applications)
{
    if (item.Status == 1)
    {
        <text> 
           /* start JS */
           data.push('@item.Id'); 
           /* end JS */
        </text>
    }
}

and in JS Ajax :

    <script>
    function checkIfSigned() {

        console.log(data);

        $.ajax({
            url: '@Url.Action("Method", "Controller")',
            type: "POST",
            data: data,
            ...
        });
    }
    </script>
Sign up to request clarification or add additional context in comments.

7 Comments

just delete this line : var data = { aid: @aid };
I have (IEnumerable<string> data) as parameter but call is not going inside it and logs internal 500 error
ok it was because of content-type. I remove it and call goes inside method but both IEnumerable<int> data or IEnumerable<string> data are null..
Try this : $.ajax({ url: '@Url.Action("Method", "Controller")', type: "POST", data: {aid: data}, ... });
I'm trying same
|

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.