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

16,345

Solution 1

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.

Solution 2

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.

Share:
16,345
antman1p
Author by

antman1p

C#, Java, XAML, and Shell Script coder. Start my own business dealing with Simulations for UAS training.

Updated on June 05, 2022

Comments

  • antman1p
    antman1p almost 2 years

    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?

  • antman1p
    antman1p about 9 years
    Is that in the _Layout.cshtml view, the GetforStatus.cshtml view, or both?
  • antman1p
    antman1p about 9 years
    Now I'm getting the error: The controller path '/' was not found or does not implement IController @Html.Action("_GetforStatus", "StatusController);
  • antman1p
    antman1p about 9 years
    I tried both and am getting error: Server Error in '/' Application. Compiler erroer message: CS1502. Invalid Arguments.
  • Brandon O'Dell
    Brandon O'Dell about 9 years
    That call can be in your layout view or the main/parent view. Also note Html.RenderAction and Html.Action are very similar. Html.Action returns a string, where as RenderAction outputs directly to the response stream and allows for better performance. Not sure whats causing your compiler error. Has any of your other code changed?
  • antman1p
    antman1p about 9 years
    No, I didn't change anything.
  • antman1p
    antman1p about 9 years
    It doesn't seem to be finding my controller. I wrote it this way in the _Layout shared view: @Html.Action("StatusController", //My controller name. "_GetforStatus" //The PartialViewResult action inside my controller.); It isn't finding the path.
  • antman1p
    antman1p about 9 years
    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'."
  • Brandon O'Dell
    Brandon O'Dell about 9 years
    Your view _GetforStatus needs to declare @model List<IntegrationWeb.models.Job> since that is what you are setting the model equal to in the controller.
  • antman1p
    antman1p about 9 years
    I did not have the "List> part around the model path, so I tried your suggestion, not I am getting a new error. CS1061:"System.Collections.Generic.List<IntegrationWeb.Model‌​s.Job>' does not contain a definition for "IS_Progress" and no extension....
  • Brandon O'Dell
    Brandon O'Dell about 9 years
    It sounds like in your _GetForStatus view you are trying to access Model.IS_Progress. Since your view is using a list for the model, you need to access IS_Progress individually from each iterated item. Try wrapping it in a @foreach(var item in Model) { <span>@item.IS_Progress</span> }
  • antman1p
    antman1p about 9 years
    Thank you much! Can you please give me an "up" on my question? Thanks again!