3

In models I have:

public class BidModel
{

    public int id { get; set; }
    public string FirstName{ get; set; }
    public string LastName{ get; set; }
    public int Price{ get; set; }
    public string Date{ get; set; }

} 

Where or how should I define a global list List<BidModel> MyList = new List<BidModel>; that I can add items in one controller then remove items from another controller, to change content of the list wherever I want.OK I know to work with database but i need this List of offers becase i dont want to much queries to base.

2
  • I think this question may be a little misguided. I originally read it wrong. What is the point of having the database if you're not using it to store your data? I can see caching data that is mostly static, but not the meat of the application. Commented Feb 10, 2011 at 22:04
  • OK i will tell all the story coz i am misunderstoud i am making a site for Auctions so i need the list for the bidHistory i hope its all clear now. I dont need all the offers in database.Only the biggest bidder. Commented Feb 10, 2011 at 22:45

4 Answers 4

4

In Mode-View-Controller, it is the controllers responsibility to hand the correct model down to the view, depending on the action you are calling. With that in mind. You don't want to create a global list with your BidModel.

Since you are using MVC 3 you can pass it down to the View quite nicely using the dynamic property ViewBag.

Consider this:

public ActionResult RetreiveAllBids()
{
    var bids = BidRepository.GetAllBids();

    ViewBag.Bids = bids.ToArray();

    return View();
}

You could of course use LINQ-to-* depending on what your ORM / Data Storage is. But this might get you an idea on how to pass the List down to the View.

Edit

I Miss-read your question a little bit, but I still think the above is valid, but depending on how you want your data to be accessable and when you want to dispose it, you have two options:

  • Static variable that lives during the whole life-cycle of your web application
  • Session variable that lives with the current user

And you might only want these accessable from certain sites, maybe you want to break those functions out and put them in a parent class, where you store the BidModel list like this:

public IEnumerable<BidModel> Bids
{
   get { return Session["Bids"] as IEnumerable<BidModel>; }
   set { Session["Bids"] = value; }
}

This way you can just derive from this parent class on the controllers that needs the functionality to add / list / remove or whatever you like to do with the bidders.

Sign up to request clarification or add additional context in comments.

Comments

1

You would most likely have a BidModel repository or some other sort of business logic manager that handled that. Your controllers would then enter through this repository for your basic CRUD operations while also allowing you to validate against your business logic.

In short, think of your model class as the business object. Your controllers would then work with your business layer to manipulate the business objects.

Comments

1

In most ASP.NET MVC applications, you really have two "model" concepts; there are the models that are consumed by views to drive what they render (sometimes called a "View Model"), and then there's the data model, typically classes that map to database tables. You won't typically be manipulating a List<> of your model objects; rather, you persist and retrieve items from a database. Look into LINQ to SQL or the Entity Framework for Microsoft technologies for your data model.

Comments

0

One option would be to use an ioc container like structuremap or ninject to control the lists lifetime. You could then pass that instance to any controllers constructor.

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.