How to use multiple DBContext models in one view for ASP.NET MVC?

What about creating custom view model class: public AnimalModel { public IEnumerable Dogs { get; set; } public IEnumerable Cats { get; set; } } Fill this model in Index and pass it to the view which will expect AnimalModel instead of enumerables Edit: Filling the model: public ActionResult Index() { using (var db = new AnimalDBContext()) { var model = new AnimalModel { Dogs = db.Dogs.ToList(), Cats = db.Cats.ToList() }; return View(model); } } View (I have never used Razor so I hope this is correct): model AnimalProject.Models. AnimalModel @foreach (var d in Model. Dogs) { @d.Name @d.

Breed } @foreach (var c in Model. Cats) { @c.Name @c. Breed }.

What about creating custom view model class: public AnimalModel { public IEnumerable Dogs { get; set; } public IEnumerable Cats { get; set; } } Fill this model in Index and pass it to the view which will expect AnimalModel instead of enumerables. Edit: Filling the model: public ActionResult Index() { using (var db = new AnimalDBContext()) { var model = new AnimalModel { Dogs = db.Dogs.ToList(), Cats = db.Cats.ToList() }; return View(model); } } View (I have never used Razor so I hope this is correct): @model AnimalProject.Models. AnimalModel @foreach (var d in Model.

Dogs) { @d. Name @d. Breed } @foreach (var c in Model.

Cats) { @c. Name @c. Breed }.

I just tried this, perhaps you can elaborate on what to do. If I specify the AnimalModel class in the @model at the top of my view, populate the AnimalModel class in my Index, when I try @foreach (var d in Model) I can't go three levels deep. So, if I write @d.

Dogs, that's all I get, I can't do @d.Dogs. Name, because the Dogs have to be enumerated as well. Does that make sense?

– Monochromatic Apr 29 at 7:58 1 You must iterate over properties of the model: foreach(var d in Model. Dogs) – Ladislav Mrnka Apr 29 at 8:08 When I try that, and add a period (Model. ), Dogs is not an option in the view.It only shows Aggregate, All, Any... etc. Perhaps I'm not filling in the model properly.

For some reason in MVC I haven't been able to iterate any class' properties using foreach. – Monochromatic Apr 29 at 8:29 How did you registered AnimalModel in the view? It looks like you have registered collection of animal models.

– Ladislav Mrnka Apr 29 at 8:34 Like this: @model IEnumerable – Monochromatic Apr 29 at 8:36.

The only solution to this problem I've found so far is to chuck both models in the ViewBag, and then render partial views for parts that use only one model. Of course, it only works if the partial views are separable - as is in your case. This allows strongly typed partial views compared to accessing the contents of the ViewBag directly.

The solution is similar to Ufuk's answer here. First put data in ViewBag from controller: ViewBag. Dogs = db.

Dogs; ViewBag. Cats = db. Cats; Then render the partial View from the view: @Html.

Partial("_ListDogs", ViewBag. Dogs) @Html. Partial("_ListCats", ViewBag.

Cats) And each partial view would be along the lines of the following (example is _ListDogs. Cshtml): @model IEnumerable @foreach (var d in Model) { @d. Name @d.

Breed }.

This seems promising, everything is set up properly but when I specify @Html. Partial("_ListDogs", ViewBag. Dogs) I get this error: System.Web.Mvc.

HtmlHelper' has no applicable method named 'Partial' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.

– Monochromatic Apr 29 at 8:37 Have a look at stackoverflow. Com/questions/4047543/… and the SO link in the first answer too. Casting the ViewBag.

Dogs to object made the problem go away for me, but I am not sure if it should be cast to IEnumerable instead. – vhallac Apr 29 at 18:08.

Multiple dbcontext models is used (without quotes):.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions