|
-
Jun 2nd, 2011, 07:45 PM
#1
[RESOLVED] Accessing Entities from another controller
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:
asp Code:
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:
asp Code:
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);
}
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Jun 3rd, 2011, 01:55 PM
#2
Hyperactive Member
Re: Accessing Entities from another controller
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)
-
Jun 3rd, 2011, 07:06 PM
#3
Re: Accessing Entities from another controller
 Originally Posted by Krokonoster
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.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Jun 4th, 2011, 09:47 AM
#4
Hyperactive Member
Re: Accessing Entities from another controller
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.
-
Jun 7th, 2011, 12:17 AM
#5
Re: Accessing Entities from another controller
I asked my lecturer and he said that I was actually to create new new instance of the MvcMusicStoreEntities in the BrowseController like so:
asp Code:
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.
Last edited by Nightwalker83; Jun 7th, 2011 at 12:50 AM.
Reason: Adding more!
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|