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
分享到:
相关推荐
- 通过`addEntity()`方法将查询结果映射到自定义的`CustomerSummary`类。 #### 六、总结 通过以上两种方式,我们不仅能够灵活地进行多表联合查询,还可以有效地将查询结果映射到Java对象中。这对于提高代码的可读性...
### **3. LINQ to SQL 语句(3) - Count/Sum/Min/Max/Avg** 这些聚合函数用于统计或计算查询结果。 #### 3.1 Count: ```csharp int londonCustomersCount = customers.Count(c => c.City == "London"); ``` 返回...
select new CustomerSummary { Name = customer.Name, City = customer.City }).ToList(); ``` 可以创建已知类型的新对象列表。 5. **筛选形式**: ```csharp var filteredNames = customers.Select(c => c....
3. **函数(003-Functions)**: 函数是可重用的SQL代码块,通常用于执行特定计算或操作。在SplendidCRM中,可能有函数用于处理日期、货币转换、计算评分等。了解这些函数有助于理解系统如何处理业务逻辑。 4. **...
select new CustomerSummary { Name = customer.Name, City = customer.City }; ``` - **筛选形式**:在投影中使用`Where`: ```csharp var query = from customer in Customers where customer.City != "London" ...
`CREATE VIEW CustomerSummary AS SELECT CustomerName, SUM(OrderTotal) FROM Orders JOIN Customers ON Orders.CustomerID=Customers.CustomerID GROUP BY Customers.CustomerID`创建一个显示每个客户总订单金额的...
- **指定类型形式**:选择到已知类型,`query = context.Customers.Select(c => new CustomerSummary { Name = c.Name, City = c.City })`。 - **筛选形式**:过滤重复元素,`query = context.Customers.Select(c ...