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>