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?
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>