Model添加类 Customers
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 6 namespace WebApplication2.Models 7 { 8 public class Customers 9 { 10 public string CustomerID { get; set; } 11 public string CompanyName { get; set; } 12 public string ContactName { get; set; } 13 public string ContactTitle { get; set; } 14 public string Address { get; set; } 15 public string City { get; set; } 16 } 17 }View Code
增加控制器 Cus
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 using WebApplication2.Models; 7 8 namespace WebApplication2.Controllers 9 { 10 public class CusController : Controller 11 { 12 // GET: Cus 13 public ActionResult Index() 14 { 15 List<Customers> customers = new List<Customers>() 16 { 17 new Customers{ CustomerID="ALFKI",CompanyName="Alfreds Futterkiste",ContactName="Maria Anders",ContactTitle="Sales Representative",Address="Obere Str. 57?",City="Berlin" }, 18 new Customers{ CustomerID="ALFKI",CompanyName="Alfreds Futterkiste",ContactName="Maria Anders",ContactTitle="Sales Representative",Address="Obere Str. 57?",City="Berlin" }, 19 new Customers{ CustomerID="ALFKI",CompanyName="Alfreds Futterkiste",ContactName="Maria Anders",ContactTitle="Sales Representative",Address="Obere Str. 57?",City="Berlin" }, 20 new Customers{ CustomerID="ALFKI",CompanyName="Alfreds Futterkiste",ContactName="Maria Anders",ContactTitle="Sales Representative",Address="Obere Str. 57?",City="Berlin" }, 21 new Customers{ CustomerID="ALFKI",CompanyName="Alfreds Futterkiste",ContactName="Maria Anders",ContactTitle="Sales Representative",Address="Obere Str. 57?",City="Berlin" }, 22 new Customers{ CustomerID="ALFKI",CompanyName="Alfreds Futterkiste",ContactName="Maria Anders",ContactTitle="Sales Representative",Address="Obere Str. 57?",City="Berlin" }, 23 new Customers{ CustomerID="ALFKI",CompanyName="Alfreds Futterkiste",ContactName="Maria Anders",ContactTitle="Sales Representative",Address="Obere Str. 57?",City="Berlin" }, 24 }; 25 return View(customers); 26 } 27 } 28 }View Code
增加视图 Index
1 @model IEnumerable<WebApplication2.Models.Customers> 2 @{ 3 ViewBag.Title = "Index"; 4 } 5 6 <h2>Customers</h2> 7 <table class="table"> 8 <tr> 9 <th>CustomerID</th> 10 <th>CompanyName</th> 11 <th>ContactName</th> 12 <th>ContactTitle</th> 13 <th>Address</th> 14 <th>City</th> 15 </tr> 16 @foreach (var item in Model) 17 { 18 <tr> 19 <td>@Html.DisplayFor(cusitem => item.CustomerID)</td> 20 <td>@Html.DisplayFor(cusitem => item.CompanyName)</td> 21 <td>@Html.DisplayFor(cusitem => item.ContactName)</td> 22 <td>@Html.DisplayFor(cusitem => item.ContactTitle)</td> 23 <td>@Html.DisplayFor(cusitem => item.Address)</td> 24 <td>@Html.DisplayFor(cusitem => item.City)</td> 25 </tr> 26 } 27 </table>View Code
显示效果