0

VisualStudio2010 tells me that it's the Model Property associated with System.Mvc.ViewDataDictionary Object which is helpful, but the docs for DataDictionary don't seem to show me what I want, in an easily understood format, at least.

Note that the following .cshtml file is in BorkedModel's View folder, but the model I want to access is in ExampleModel's Model folder. This exact code would work if the file itself was in the ExampleModel's View folder, but not in a different model's View folder.

ProjectTopDirectory/Views/BorkedModel/index.cshtml

@model IEnumerable<ExampleMVC4.Models.ExampleModel> 

    @foreach (var item in Model)
{
    <HTML here/>
}

Is there a solution to what I'm doing, or should I just keep models in their own view, and that's it? Is there a way to access one Model from another Model's View?

Community
  • 1
  • 1
TankorSmash
  • 12,186
  • 6
  • 68
  • 106
  • Models don't have views. Controller actions have views. You should be able to use any model in a view so long as you pass it from the controller action to the view. If you need multiple models, create a dedicated view model object that contains them and pass *that* to the view. – Major Productions Nov 18 '12 at 03:04
  • You might like to read this [MVC4 Tutorial](http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4). There is also a free [TekPub Screencast](http://tekpub.com/productions/aspmvc) if you're interested. – Mightymuke Nov 18 '12 at 03:11
  • @Mightymuke Thanks, it's actually the tutorial I'm working on, taken right from http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/accessing-your-models-data-from-a-controller Ctrl-F for "When you created the movie controller, Visual Studio Express automatically included the following" and the following code is what I'm talking about. – TankorSmash Nov 18 '12 at 03:11
  • @KevinM1 In the BorkedModelController, instead of calling `return View();` you're saying I return `View(ExampleModel)` instead? I tried to use `return View(Project.Models.ExampleModel` and got an `type is not valid in this context` error. And just trying `return View(ExampleModel)` can't understand what `ExampleModel` is without that first bit. – TankorSmash Nov 18 '12 at 03:14
  • 1
    @Tank - you dont return the type, you return an instance of it. eg, `return View(model)`, or in this case, `return View(movie);` – Mightymuke Nov 18 '12 at 03:20
  • @Mightymuke Alright, I'm starting to understand. You're talking about something like this: http://stackoverflow.com/a/1842340/541208 I want to iterate over all the `movies` in my database, and so it seems like I will have to pass an `IEnumerator` filled with `movie` instances, to the View instead of expecting the View to do it then, like it does in the original tutorial, is that correct? – TankorSmash Nov 18 '12 at 03:24
  • @Tank No, that view displays only one movie. I think you're getting mixed up with `Index` which returns `View(db.Movies.ToList());`. The model for that view is `IEnumerable` – Mightymuke Nov 18 '12 at 03:27
  • @Mightymuke Thank you! You helped me figure it out, HTX9 just wrote it out. I owe ya one. – TankorSmash Nov 18 '12 at 03:32

1 Answers1

2

To use a strongly typed model in a view you need to pass the model to the view from the controller:

public ActionResult Index()
{
    YourDBContext db = new YourDBContext();
    IEnumerable<ExampleModel> model = db.GetDataFromDatabase();
    return View(model); // Pass the model to the view
}

Then in your view you specify the model, which should allow you to access the data:

@model IEnumerable<NextFlicksMVC4.Models.ExampleModel>

@foreach (var item in Model)
{
    // Your HTML
}

UPDATE: Your controller action method should be retrieving the data from the database and then passing it to the View. The view's only job is to display the data to the user.

HTX9
  • 1,717
  • 3
  • 15
  • 27
  • Just one question, otherwise this looks like what I need: Is `db` the `DBContext` object? I can't seem to figure this one out just yet. – TankorSmash Nov 18 '12 at 03:27
  • @TankorSmash Yes, db is your DBContext object. – HTX9 Nov 18 '12 at 03:30
  • Ah there you go, never made the instance of the class in this controller. Thank you. – TankorSmash Nov 18 '12 at 03:31
  • For everyone else, like MightMuke said, it's `return View(db.Movies.ToList())`, there's no `GetDataFromDatabase` for my personal issue. Thanks again to everyone. – TankorSmash Nov 18 '12 at 03:37