@{
Layout = null;
} <!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>示例</title>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
//将数据对象转换为 JSON 格式,是为了在网页中通过使用JS将数据作为文本进行处理
var data = @Html.Raw(Json.Encode(ViewBag.Data));
for (var i = 0; i < data.length; i++) {
var model=data[i];
console.log("编号:"+model.id+" 姓名:"+model.name+" 性别:"+model.sex);
}
</script>
</head>
<body>
<div>
</div>
</body>
</html>
public ActionResult Sample()
{
List<SampleData> list = new List<SampleData>()
{
new SampleData{ id=, name="张三", sex="男"},
new SampleData{ id=, name="李四", sex="男"},
new SampleData{ id=, name="李倩", sex="女"},
new SampleData{ id=, name="张燕", sex="女"}
};
ViewBag.Data = list;
return View();
} public class SampleData
{
public int id { get; set; }
public string name { get; set; }
public string sex { get; set; }
}