读书人

三、CustomerSummary

发布时间: 2012-11-10 10:48:50 作者: rapoo

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; }    }}


对实体类的操作类 CustomerSummaryManager.cs
这个类,相当于从数据库中读取数据,不过我没写数据库这一块,所以直接在这里写入初始值。
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;        }    }}

实际情况,public IEnumerable<CustomerSummary> GetAll()函数,要从数据库中将数据读取出来。

创建Controller和View
CustomSummaryController.cs
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);        }    }}

示图Index.aspx
<%@ 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>

注意问题:Inherits="System.Web.Mvc.ViewPage<IEnumerable<MVC2_2.Models.CustomerSummary>>"
一定要加入名字空间MVC2_2.Models
最终结果:
http://localhost:13791/CustomSummary

总结:
对数据的操作并没有包括在MVC中。
数据操作可以定义接口,然后针对不同的数据库实现接口。
虽然我这里是直接赋值 ,但同从数据库里读取道理是相同的。主要是理解MVC这条线的实现过程,可以更深一步扩展,加上数据库,同时加上读取操作。
很关键要理解的是:
IEnumerable<CustomerSummary> summaries = _customerSummaries.GetAll();
return View(summaries);

传入视图后怎么展现出来:
<% foreach (var summary in Model)
{ %>
<tr>
<td>
<%=summary.Name%>
...

2011-5-17 15:06 danny

读书人网 >编程

热点推荐