【发布时间】:2015-10-12 20:54:51
【问题描述】:
我在我的控制器中创建一个字符串列表并将其赋值给一个字符串列表。我想将值列表分配给 JavaScript 变量。我怎样才能做到这一点?
控制器:
List<string> fruits = new List<string>();
list.Add('Apple');
list.Add('Banana');
list.Add('Kiwi');
ViewBag.FruitList = fruits;
查看:
var fruitList = '@ViewBag.FruitList';
当我运行程序时,fruitList 以System.Collections.Generic.List 1[System.String] 的形式返回
【问题讨论】:
-
可能重复。你看过这个stackoverflow.com/questions/6192950/…
-
从控制器返回 json,然后在客户端返回
$.getJSON() -
为什么 List 需要这个但说带有字符串的 ViewBag 不需要?
标签: javascript c# asp.net-mvc asp.net-mvc-3