3.1、集合列表遍历
控制器:
/// <summary> /// 默认测试集合 /// </summary> /// <returns></returns> public List<GirlSix> DefaultList() { List<GirlSix> lists = new List<GirlSix>(); lists.Add(new GirlSix() { id = System.Guid.NewGuid().ToString("N"), createDate = DateTime.Now, nickName = "董新颖", age = 20, introduce = "郭老师关门弟子之一。" }); lists.Add(new GirlSix() { id = System.Guid.NewGuid().ToString("N"), createDate = DateTime.Now, nickName = "王笑涵", age = 19, introduce = "北方有佳人,绝世而独立。" }); lists.Add(new GirlSix() { id = System.Guid.NewGuid().ToString("N"), createDate = DateTime.Now, nickName = "牛龙珠", age = 21, introduce = "笑若桃花三月开,清风徐徐醉颜来。" }); lists.Add(new GirlSix() { id = System.Guid.NewGuid().ToString("N"), createDate = DateTime.Now, nickName = "闫春娜", age = 21, introduce = "珠缨旋转星宿摇,花蔓抖擞龙蛇动。" }); lists.Add(new GirlSix() { id = System.Guid.NewGuid().ToString("N"), createDate = DateTime.Now, nickName = "刘梓佳", age = 20, introduce = "明眸善睐,辅靥承权,瑰姿艳逸,怡静体闲,端的是好一个花王,富贵的牡丹。" }); lists.Add(new GirlSix() { id = System.Guid.NewGuid().ToString("N"), createDate = DateTime.Now, nickName = "魏慧娟", age = 21, introduce = "脉脉眼中波,盈盈花盛处。" }); return lists; }
//传递值视图 ViewBag.list = DefaultList();
视图:
@{ ViewBag.Title = "主页面"; } @* 视图中 *@ <table class="table table-bordered table-hover table-striped"> <tr class="info"> <td>编号</td> <td>创建时间</td> <td>昵称</td> <td>年龄</td> <td>简介</td> </tr> @foreach (var item in ViewBag.list) { <tr> <td>@item.id</td> <td>@item.createDate</td> <td>@item.nickName</td> <td>@item.age</td> <td><pre>@item.introduce</pre></td> </tr> } </table>