好吧,如果你使用的只是一個字符串列表,然後
這會做
$(document).ready(function() {
@{List<string> listFromController = (List<string>)ViewData["List"];}
var myArray = [
@for (int i = 0; i < listFromController.Count; i++)
{
@: '@(listFromController[i])',
}
]
});
但如果你是通過列表中的另一類型的,而不是字符串像一個學生員工或用戶,您將需要以下
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的視圖引擎語法:
<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>
什麼是在您的ViewData [「列表」] –
可能重複的[jQuery:如何遍歷/迭代對象列表](http://stackoverflow.com/questions/30730055/jquery-how-to-遍歷遍歷對象列表) –
不要忘記upvote或作爲答案,如果它爲你工作... –