error 'System.InvalidOperationException' conflict when joining two views?

2

I have this view that you need the Audit model but the masterPage has @Html.Partial("/Views/component/panelNavbar.cshtml") conflict with the Audit model with ConfMaster how to solve this?

Index Audit

@model IEnumerable<CSHLPA.Models.Audit>

@{
Layout = "/Views/component/MasterPage.cshtml";
}

PanelNavbar

@using CSHLPA.Helpers
@model CSHLPA.Models.ConfMaster
<aside id="left-panel" class="left-panel">
    <nav class="navbar navbar-expand-sm navbar-default">
     //mas codigo....

Complete Error

  

An exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll but was not controlled in the user's code   Additional information: The model item passed into the dictionary is of type 'System.Collections.Generic.List'1 [CSHLPA.Models.Audit]', but this dictionary requires a model item of type 'CSHLPA.Models.ConfMaster'.

    
asked by Luis Acosta 11.04.2018 в 15:25
source

2 answers

0

I recommend that in MasterPage.cshtml you do not use a Partial View, but a Child Action. Replace @Html.Partial ("/ Views / component / panelNavbar.cshtml") by @ Html.Action ("ShowNavBar", "NavBar"). Create a NavBarController with an action method ShowNavBar:

public class NavBarController : Controller {
    public ActionResult ShowNavBar() {
        return View("~/Views/Shared/panelNavbar.cshtml", new ConfMaster{ Nombre = "ConfMaster1" });
    }
}

Since the model of a master page is independent of the view where it is called, it must load its data from a child Action.

I hope I help you

    
answered by 11.04.2018 в 18:07
0

Remember that when you invoke a partial you can pass it a model as a parameter

@Html.Partial("/Views/component/panelNavbar.cshtml", model)

If you analyze this article

@ Html.Partial Example - Partial View Render using @ Html.Partial in ASP.Net MVC

You will see how the model in the main view has the data to be able to send to the partial view when you use

<td>@Html.Partial("Details", customer)</td> 

Usually the master should not depend on a model

    
answered by 11.04.2018 в 20:36