Results 1 to 5 of 5

Thread: [RESOLVED] Accessing Entities from another controller

  1. #1

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Resolved [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:
    1. using MvcMusicStore.Controllers;
    2.  
    3. namespace MvcMusicStore.Controllers
    4. {
    5.     public class BrowseController : Controller
    6.     {
    7.         //
    8.         // GET: /Browse/
    9.  
    10.         public ActionResult Browse(string genre)
    11.         {
    12.          //Retrieve genre and its associated albums from the database
    13.             var genreModel = storeDB.Genres.Include("Albums")
    14.                 .single(g => g.Name == genre);
    15.         }
    16.  
    17.     }
    18. }


    StoreController:
    asp Code:
    1. public class StoreController : Controller
    2.     {
    3.         MvcMusicStoreEntities StoreDB = new MvcMusicStoreEntities();
    4.         //
    5.         // GET: /Store/
    6.  
    7.          public ActionResult Index()
    8.         {
    9.             //Retrieve list of genres from database
    10.             var genres = from genre in StoreDB.Genres
    11.                          select genre.Name;
    12.  
    13.             //Create the view model
    14.             var viewModel = new StoreIndexViewModel
    15.             {
    16.                 NumberOfGenres = genres.Count(),
    17.  
    18.             };
    19.             return View(viewModel);
    20.         }
    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

  2. #2
    Hyperactive Member Krokonoster's Avatar
    Join Date
    Jan 2010
    Location
    Cape Town
    Posts
    448

    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)


  3. #3

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Accessing Entities from another controller

    Quote Originally Posted by Krokonoster View Post
    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

  4. #4
    Hyperactive Member Krokonoster's Avatar
    Join Date
    Jan 2010
    Location
    Cape Town
    Posts
    448

    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.


  5. #5

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    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:
    1. MvcMusicStoreEntities storeDB = new MvcMusicStoreEntities();
    2.         public ActionResult Browse(string genre)
    3.         {
    4.          //Retrieve genre and its associated albums from the database
    5.             var genreModel = storeDB.Genres.Include("Albums").Single(g => g.Name == genre);
    6.             var viewModel = new StoreBrowseViewModel()
    7.             {
    8.                 Genre = genreModel,
    9.                 Albums = genreModel.Albums.ToList()
    10.             };
    11.             return View(viewModel);
    12.            
    13.         }

    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
  •  



Click Here to Expand Forum to Full Width