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 = "johndoe@gmail.com",
OrdersPlaced = 5,
DateOfLastOrder = DateTime.Parse("5/3/2007")
});
customers.Add(new SVC
{
ID = 2,
FirstName = "Jane",
LastName = "Doe",
Phone = "2222222222",
Email = "janedoe@gmail.com",
OrdersPlaced = 3,
DateOfLastOrder = DateTime.Parse("4/5/2008")
});
customers.Add(new SVC
{
ID = 3,
FirstName = "John",
LastName = "Smith",
Phone = "3333333333",
Email = "johnsmith@yahoo.com",
OrdersPlaced = 25,
DateOfLastOrder = DateTime.Parse("4/5/2000")
});
customers.Add(new SVC
{
ID = 4,
FirstName = "Eddie",
LastName = "Murphy",
Phone = "4444444444",
Email = "eddiem@yahoo.com",
OrdersPlaced = 1,
DateOfLastOrder = DateTime.Parse("4/5/2003")
});
customers.Add(new SVC
{
ID = 5,
FirstName = "Ziggie",
LastName = "Ziggler",
Phone = null,
Email = "ziggie@hotmail.com",
OrdersPlaced = 0,
DateOfLastOrder = null
});
customers.Add(new SVC
{
ID = 6,
FirstName = "Michael",
LastName = "J",
Phone = "666666666",
Email = "ziggie@hotmail.com",
OrdersPlaced = 5,
DateOfLastOrder = DateTime.Parse("12/3/2007")
});
}
}
}
这些代码是我从互联网上获得的一个例子。
在这种情况下,数据会在显示之前创建并保存在会话中。
我想问的问题是如果我想从表中加载数据?
我在这里是新手..请帮助:))
感谢b4提前..
答案 0 :(得分:2)
我目前在ASP.NET MVC中加载数据等操作的方式是使用Repository模式。
出于本练习的目的,我建议您查看类似Entity Framework
的内容然后,您的控制器将引用“上下文”,这是您将用于调用数据库的对象。
e.g。
public class PersonController : Controller
{
private MyEntitiyFrameworkDataContext context = new MyEntitiyFrameworkDataContext();
public ActionResult Index()
{
return View(context.Persons);
}
}