Razor视图下服务器代码给Javascript变量赋值

namespace Razor.Controllers
{
   
public class JSController : Controller
   
{
       
public ActionResult Index()
       
{
           
List<string> FriendsId = new List<string> { "S1", "S2", "S3", "S4" };
           
ViewBag.FriendsId = FriendsId;
           
return View();
       
}
   
}
}
@{
    ViewBag.Title = "Index";
}
<h2>Index</h2> <script type="text/javascript"> var array = new Array(); @{    string message=""; }
@for (int i = 0; i < 3; i++) {     message += "array[" + i + "]=\"" + ViewBag.FriendsId[i] + "\";"; } @MvcHtmlString.Create(message); alert(array[0]); </script>
posted on 2013-03-28 18:06  一路前行  阅读(992)  评论(0编辑  收藏  举报