0

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 = "[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")
        });

    }

 }
}

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..

1 Answer 1

2

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);
     }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.