How do I return a View and a Partial View to the Index Layout in ASP.NET MVC 4?

asp.net-mvc-4 c# entity-framework-6 partial-views razor

Question

Both my home controller and my "StatusController" create a new instance of a DBEntities. The Home Controller returns a _db.VMs ViewData.Model to the _Layout.cshtml view and the Status Controller returns a _db.Jobs ViewDataModel to the _GetForStatus.cshtml view as a partial view. I call the VMs ViewData model to the _Layout view like so:

 @foreach (var m in ViewData.Model)
      {
           <li><a href="#">@m.Name</a></li>
      }

This works fine, populating the dropdown list with names of the VMs from the DB. The _GetforStatus. The Home Controller piece for this is written like so:

 public ActionResult index()
      {
           _db = new IntegrationDBEntitires();
           ViewData.Model = _db.VMs.ToList();
           return View();
      }

The StatusController is written like so:

 public PartialViewResult _GetforStatus()
      {
           _db = new OntegrationDBEntities();
           ViewData.Model = _db.Jobs.ToList();
           return PartialView();
      }

The _GetforStatus view is written like so:

 @model IntegrationWeb.Models.Job
      <div class="progress progress-striped active">
           <div class="progress-bar" style="width: @((Model.IS_Progress / Model.IS_Total)*100)%"></div>
      </div>
 @Html.Action("_GetforStatus", "StatusController")

This is called in the _Layout view like so:\

 @Html.Partial("~/Views/_GetforStatus.cshtml")

I am getting an error here. "The model item passed into the dictionary is of type 'System.Collections.Generic.List' 1[IntegrationWeb.models.VM]', but the dictionary requires model item type 'IntegrationWeb.Models.Job'. It seems as though there is a conflict with pulling two different DBEntities into the Layout view.
How do I return a View and a Partial View to the Index Layout in ASP.NET MVC 4?

Update: I just put the Action in the Home Controller, since I can't figure out why it won't find my Controller "StatusController". Now I am getting a different error using @{Html.RenderAction("_GetforStatus");} "The Model Item passed into the dictionary is type 'System.Collections.Generic.List '1[IntegrationWeb.models.Job]', but this dictionary requires a model item of type 'IntegrationWeb.models.Job'." Anyone know what is going on here?

1
2
2/17/2015 6:40:20 PM

Accepted Answer

A partial view inherits the Model from the parent view, unless you specifically pass a property, or create another object to pass to it. They are also processed of the same request. So in your example, the controller action for the partial view isn't even being executed. What you need to do instead is use RenderAction.

@{ Html.RenderAction("_GetforStatus","ControllerName"); }

This will then allow you to to execute a view with its own model and separate request.

6
2/17/2015 4:07:29 PM

Popular Answer

Instead of

@Html.Partial("~/Views/_GetforStatus.cshtml")

simply do

@Html.Action("_GetForStatus", "MyController")

Html.Partial doesn't actually hit the action, it just renders the cshtml file.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow