Controllers
位置:Controllers
命名:HomeController
// Views/Home/Index public ActionResult Index() { ViewBag.Mydata = "Hello World"; return View(); } //数据列表 public ActionResult Dic() { " }; var dic = new Dictionary<string, string>(); dic.Add(", "Lee"); dic.Add(", "Mp"); ViewBag.dic = dic; ViewData["str"] = str; return View(); } //使用Model public ActionResult List() { var list = new List<Test> { ",Sex="男"}, ",Sex="女"}, ",Sex="人妖"} }; ViewBag.Title = "列表和Layer"; return View(); } //使用数据集 public ActionResult Form() { DataSet ds = Lee.Common.CreateDataSet(); ViewBag.Data = ds.Tables[]; return View(); }
View
常规: @{ ViewBag.Title = "Index"; } <h2>@(ViewBag.Mydata),123</h2> 循环遍历1: @{ ViewBag.Title = "Dic"; var dic = ViewBag.dic; var str = ViewData["str"]; } @{ foreach(KeyValuePair<string,string> aa in dic) { <h1>@aa.Key : @aa.Value</h1> } } @{ foreach (var bb in str as List<string>) { <h1>@bb</h1> } } 循环遍历2: @{ Layout = "~/Views/Common/Layer.cshtml"; } @using MvcApplication1.Models; @model IEnumerable<Test> @foreach (var item in Model) { <p>@item.Sex</p> } 数据集遍历 @using System.Data; @using Lee; @{ ViewBag.Title = "Form"; Layout = "~/Views/Common/Layer.cshtml"; DataTable dt = (DataTable)ViewBag.Data; } @{ <table class="table table-hover"> <thead> <tr> @for (int i = 0; i < dt.Columns.Count;i++ ) { <th>@dt.Columns[i].ToString()</th> } </tr> </thead> <tbody> @for (int j = 0; j < dt.Rows.Count;j++ ) { <tr> <td>@dt.Rows[j][dt.Columns[0].ToString()]</td> <td>@dt.Rows[j][dt.Columns[1].ToString()]</td> <td>@dt.Rows[j][dt.Columns[2].ToString()]</td> <td>@dt.Rows[j][dt.Columns[3].ToString()]</td> </tr> } </tbody> </table> }
Model
Test.cs public class Test { private string id; public string Id { get { return id; } set { id = value; } } private string money; public string Money { get { return money; } set { money = value; } } private string sex; public string Sex { get { return sex; } set { sex = value; } } private string code; public string Code { get { return code; } set { code = value; } } }