3、CustomerSummary
CustomerSummary
本例跨度比较大,再加上看的是英文版,很多细节没有仔细看明白。
我只是想办法达到了效果,当然也是达到了MVC分层。
按教程,实体类,
CustomerSummary.cs
using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace MVC2_2.Models{ public class CustomerSummary { public string Name { get; set; } public bool Active { get; set; } public string ServiceLevel { get; set; } public string OrderCount { get; set; } public string MostRecentOrderDate { get; set; } }}using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace MVC2_2.Models{ public class CustomerSummaryManager { public IEnumerable<CustomerSummary> GetAll() { List<CustomerSummary> list = new List<CustomerSummary>(); CustomerSummary cs = new CustomerSummary(); cs.Name = "John Smith"; cs.Active = true; cs.ServiceLevel = "Standard"; cs.OrderCount = "42"; cs.MostRecentOrderDate = DateTime.Now.AddYears(-8).ToString(); list.Add(cs); cs = new CustomerSummary(); cs.Name = "Susan Power"; cs.Active = false; cs.ServiceLevel = "Standard"; cs.OrderCount = "1"; cs.MostRecentOrderDate = DateTime.Now.AddYears(-7).ToString(); list.Add(cs); cs = new CustomerSummary(); cs.Name = "Danny Huang"; cs.Active = true; cs.ServiceLevel = "Premier"; cs.OrderCount = "7"; cs.MostRecentOrderDate = DateTime.Now.AddYears(-9).ToString(); list.Add(cs); return list; } }}using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using MVC2_2.Models;namespace MVC2_2.Controllers{ public class CustomSummaryController : Controller { // // GET: /CustomSummary/ CustomerSummaryManager _customerSummaries = new CustomerSummaryManager(); public ActionResult Index() { IEnumerable<CustomerSummary> summaries = _customerSummaries.GetAll(); return View(summaries); } }}<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MVC2_2.Models.CustomerSummary>>" %><asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">Index</asp:Content><asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2>Index</h2> <table> <tr> <th> Name </th> <th> Active? </th> <th> Service Level </th> <th> Order Count </th> <th> Most Recent Order Date </th> </tr> <% foreach (var summary in Model) { %> <tr> <td> <%=summary.Name%> </td> <td> <%=summary.Active ? "Yes" : "No"%> </td> <td> <%=summary.ServiceLevel%> </td> <td> <%=summary.OrderCount%> </td> <td> <%=summary.MostRecentOrderDate%> </td> </tr> <%} %> </table></asp:Content>