3、CustomerSummary

CustomerSummary

本例跨度比较大,再加上看的是英文版,很多细节没有仔细看明白。
我只是想办法达到了效果,当然也是达到了MVC分层。

按教程,实体类,
CustomerSummary.cs
[code]
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; }
}
}
[/code]

对实体类的操作类 CustomerSummaryManager.cs
这个类,相当于从数据库中读取数据,不过我没写数据库这一块,所以直接在这里写入初始值。
[code]
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;
}
}
}
[/code]
实际情况,public IEnumerable<CustomerSummary> GetAll()函数,要从数据库中将数据读取出来。

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

}
}
[/code]
示图Index.aspx
[code]
<%@ 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>
[/code]
注意问题: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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值