Ok if you are using just a list of strings then
this will do
$(document).ready(function () {
@{List<string> listFromController = (List<string>)ViewData["List"];}
var myArray = [
@for (int i = 0; i < listFromController.Count; i++)
{
@: '@(listFromController[i])',
}
]
});
But if you are passing list of another type rather than string like an student Employee or a user you will need the following
Please use the appropriate class that you have passed and the
properties suppose "UserName" could be "FirstName" "EmpId" or what ever
$(document).ready(function () {
@{ var listFromController = (List<KnockoutJSWebApi.Models.LoginViewModel>)ViewData["list"];}
var totalArray = [];
@for (int i = 0; i < listFromController.Count; i++)
{
<text>
var thisArray= {
'username': '@(listFromController[i].UserName)',
'password': '@(listFromController[i].Password)'
};
totalArray.push(thisArray);
</text>
}
});
Aspx View Engine syntax:
<script>
$(document).ready(function () {
<% List<string> listFromController = (List<string>)ViewData["List"]; %>
var myArray = [
<% for (int i = 0; i < listFromController.Count; i++){ %>
'<%: listFromController[i] %>',
<% } %>
]
debugger;
});
</script>