tags:

views:

234

answers:

3

I am currently learning ASP.NET MVC so please excuse my question if it has been asked or seems rather simple, but if I could get some help I would greatly appreciate it.

I am trying to return two different repositories to the View. I am going through ASP.NET MVC's tutorials and I thought I would try taking it a step further. I can display Movies from the Movie table in the database just fine but I also want to display data from the Actors table on the same as well and I am not sure how to go about doing this. For displaying the Movies I was following the Repository pattern.

I hope this makes sense.

Thanks,

+2  A: 
ViewData["Movies"] = //Function to get Movies;
ViewData["Actors"] = //Function to get Actors;

return View();

That's what I'm doing in my app to pass multiple sets of data to the View.

EDIT:

Or, if you have relations set up in your database you could do something like:

var movies = /*Function to get Movies*/ as Movies;
movies.Actors.Load();

That will accomplish the same thing assuming you have foriegn keys set up in your tables.

And then in your view you would do:

<%
   var movies = ViewData["Movies"] as Movies;
   var actors = ViewData["Actors"] as Actors;
%>
dhulk
I would try to avoid hardcoded strings to access ViewData properties. If you're going to store it in ViewData, create a custom model class so it's strong typed.
Kevin Pang
That does seem a lot better actually (I'm not sure why, but it does), I'm assuming either way would work as mine isn't broken or anything. I was having the same problem and I saw on Scott Guthrie's blog that this was how he was doing it for the sample app he had. I do like your way better though.
dhulk
+10  A: 

Create a new class that has both a list of Movies and Actors in it:

public class MoviesAndActorsModel
{
    public IList<Movie> Movies { get; set; }
    public IList<Actor> Actors { get; set; }
}

Then, in your controller action, instantiate an object of type MoviesAndActorsModel that is populated from your repository:

public ActionResult List()
{
    MoviesAndActorsModel model = new MoviesAndActorsModel();

    model.Movies = _repository.GetMovies();
    model.Actors = _repository.GetActors();

    return View(model);
}

Now make sure your view inherits from ViewPage<MoviesAndActorsModel> and you should be able to access both the movies and actors like so:

<% foreach (Movie movie in Model.Movies) { %>
    <%= movie.Title %>
<% } %>

<% foreach (Actor actor in Model.Actors) { %>
    <%= actor.Name %>
<% } %>
Kevin Pang
I recomend this approach but I would suffix the new class with ViewData and not Model.
Andrei Rinea
I agree, this approach if favorable.
Chad Moran
This is better, I haven't actually changed mine yet to do this, but it has led me to go through some refactoring which has thus far eliminated an entire routine that I didn't really need. Also, the ASP.NET website recently added a bunch of new tutorials: http://www.asp.net/learn/mvc/
dhulk
A: 

There's a tutorial on http://asp.net/mvc, which demonstrates a variation of dhulk's solution for a slightly different problem: how to return some common data that is used by each view (e.g. by the master page) from each action method without having a lot of duplicated code.

The tutorial introduces a so-called application controller (simply a base class for all controllers) which handles returning the common/shared data to the view.

M4N