PDA

Click to See Complete Forum and Search --> : VS 2010 [RESOLVED] Accessing Entities from another controller


Nightwalker83
Jun 2nd, 2011, 07:45 PM
Hi,

How do I access an entity define in one controller say "StoreController" and use it in another controller called "BrowseController"?

This is what I have tried so far:

BrowseController:

using MvcMusicStore.Controllers;

namespace MvcMusicStore.Controllers
{
public class BrowseController : Controller
{
//
// GET: /Browse/

public ActionResult Browse(string genre)
{
//Retrieve genre and its associated albums from the database
var genreModel = storeDB.Genres.Include("Albums")
.single(g => g.Name == genre);
}

}
}



StoreController:

public class StoreController : Controller
{
MvcMusicStoreEntities StoreDB = new MvcMusicStoreEntities();
//
// GET: /Store/

public ActionResult Index()
{
//Retrieve list of genres from database
var genres = from genre in StoreDB.Genres
select genre.Name;

//Create the view model
var viewModel = new StoreIndexViewModel
{
NumberOfGenres = genres.Count(),

};
return View(viewModel);
}

Krokonoster
Jun 3rd, 2011, 01:55 PM
What do you mean?
Controllers are standalone classes that act as...well...conducting data between your entities / view models and the views.
You can always create a new instance of your model in another controller....but not sure about what you really want to do.

Or maybe you mean you don't want to hit the database twice doing exactly the same thing?
Just move the query out to a "repository class" and cache the method returning the data?

But as for actually using one controller action from another controller, you should not be able to do that (if you can, your controller have responsibilities it should not have)

Nightwalker83
Jun 3rd, 2011, 07:06 PM
What do you mean?
Controllers are standalone classes that act as...well...conducting data between your entities / view models and the views.
You can always create a new instance of your model in another controller....but not sure about what you really want to do.


It is for a case study which, we have been given all the code, etc for. I am just stuck on a part in appears that my lecturer wants us to modify StoreDB to "Albums" from within the browse controller when we created the connection in the store controller. I thought there might have been a global class I was suppose to allow both the BrowseController and StoreController access to in-order do I am trying to achieve above.

Krokonoster
Jun 4th, 2011, 09:47 AM
If you have a single method (in a Class specially for that) that does the actual data retrieval, both Controllers can use that. Sounds like he's after something like that.

Nightwalker83
Jun 7th, 2011, 12:17 AM
I asked my lecturer and he said that I was actually to create new new instance of the MvcMusicStoreEntities in the BrowseController like so:


MvcMusicStoreEntities storeDB = new MvcMusicStoreEntities();
public ActionResult Browse(string genre)
{
//Retrieve genre and its associated albums from the database
var genreModel = storeDB.Genres.Include("Albums").Single(g => g.Name == genre);
var viewModel = new StoreBrowseViewModel()
{
Genre = genreModel,
Albums = genreModel.Albums.ToList()
};
return View(viewModel);

}


Edit:

The above code belongs in the storeController I have no idea why I included a BrowseController in the project because it wasn't needed.