2010-04-23 86 views
0

它我再...... 我有一些像这样的代码..ASP NET MVC(从数据库中装载的数据)

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace MvcGridSample.Models 
{ 
public class CustomerService 
{ 
    private List<SVC> Customers 
    { 
    get 
    { 
    List<SVC> customers; 
    if (HttpContext.Current.Session["Customers"] != null) 
    { 
    customers = (List<SVC>) HttpContext.Current.Session["Customers"]; 
    } 
    else 
    { 
    //Create customer data store and save in session 
    customers = new List<SVC>(); 

    InitCustomerData(customers); 

    HttpContext.Current.Session["Customers"] = customers; 
    } 

    return customers; 
    } 
    } 




    public SVC GetByID(int customerID) 
    { 
    return this.Customers.AsQueryable().First(customer => customer.seq_ == customerID); 
    } 


    public IQueryable<SVC> GetQueryable() 
    { 
    return this.Customers.AsQueryable(); 
    } 


    public void Add(SVC customer) 
{ 
    this.Customers.Add(customer); 
    } 


    public void Update(SVC customer) 
    { 

    } 


    public void Delete(int customerID) 
    { 
    this.Customers.RemoveAll(customer => customer.seq_ == customerID); 
    } 


    private void InitCustomerData(List<SVC> customers) 
    { 
     customers.Add(new SVC 
     { 
      ID = 1, 
      FirstName = "John", 
      LastName = "Doe", 
      Phone = "1111111111", 
      Email = "[email protected]", 
      OrdersPlaced = 5, 
      DateOfLastOrder = DateTime.Parse("5/3/2007") 
     }); 

     customers.Add(new SVC 
     { 
      ID = 2, 
      FirstName = "Jane", 
      LastName = "Doe", 
      Phone = "2222222222", 
      Email = "[email protected]", 
      OrdersPlaced = 3, 
      DateOfLastOrder = DateTime.Parse("4/5/2008") 
     }); 


     customers.Add(new SVC 
     { 
      ID = 3, 
      FirstName = "John", 
      LastName = "Smith", 
      Phone = "3333333333", 
      Email = "[email protected]", 
      OrdersPlaced = 25, 
      DateOfLastOrder = DateTime.Parse("4/5/2000") 
     }); 


     customers.Add(new SVC 
     { 
      ID = 4, 
      FirstName = "Eddie", 
      LastName = "Murphy", 
      Phone = "4444444444", 
      Email = "[email protected]", 
      OrdersPlaced = 1, 
      DateOfLastOrder = DateTime.Parse("4/5/2003") 
     }); 


     customers.Add(new SVC 
     { 
      ID = 5, 
      FirstName = "Ziggie", 
      LastName = "Ziggler", 
      Phone = null, 
      Email = "[email protected]", 
      OrdersPlaced = 0, 
      DateOfLastOrder = null 
     }); 


     customers.Add(new SVC 
     { 
      ID = 6, 
      FirstName = "Michael", 
      LastName = "J", 
      Phone = "666666666", 
      Email = "[email protected]", 
      OrdersPlaced = 5, 
      DateOfLastOrder = DateTime.Parse("12/3/2007") 
     }); 

    } 

} 
} 

这些代码是我从网上找来的例子..
在这种情况下,数据创建并保存在会话之前显示..
我想问的事情是如何如果我想从表中加载数据?
我'这里是新手..请帮助:)

感谢b4针对提前..

回答

2

我现在去使用Repository模式做这样的ASP.NET MVC装载数据操作的方式。

在这个练习中,我建议在寻找的东西像Entity Framework

你的控制器则必须将“上下文”,这是你会使用,使调用数据库的对象的引用的目的。

例如

public class PersonController : Controller 
{ 
    private MyEntitiyFrameworkDataContext context = new MyEntitiyFrameworkDataContext(); 

    public ActionResult Index() 
    { 
     return View(context.Persons); 
    } 
}