ASP NET MVC (loading data from database)
its me again... i have some code like this..
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开发者_JS百科,
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")
});
}
}
}
those codes is an example that i've got from the internet..
in that case, the data is created and saved in session before its shown.. the things that i want to ask is how if i want to load the data from table? i'am a newbie here.. please help :)thank b4 for advance..
The way I currently go about doing operations like loading data in ASP.NET MVC is using the Repository pattern.
For the purpose of this exercise I would suggest looking at something like the Entity Framework
Your controller will then have a reference to the "context" which is the object that you will use to make calls to the database.
e.g.
public class PersonController : Controller
{
private MyEntitiyFrameworkDataContext context = new MyEntitiyFrameworkDataContext();
public ActionResult Index()
{
return View(context.Persons);
}
}
精彩评论