2
votes

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?

2
show your layout view and main viewEhsan Sajjad
What does partialView() do? This is not a standard method...Erik Funkenbusch
I'm not sure what you're asking. It is my understanding that it returns the partialview.antman1p
There is no method in MVC called partialView(), there is PartialView()... is this just a typo?Erik Funkenbusch
Yes, It is a typo. I am rewritting from another computer.antman1p

2 Answers

6
votes

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.

3
votes

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.