Call Partial with IEnumerable within a page that has its IEnumerable asp.net mvc?

1

In my layout I have an option where I show some notifications, so include a partial layout in the layout:

@Html.Partial("_PartialNotificacoes")
@model IEnumerable<Generico.Dominio.TB_NOTIFICACAO>     

@if (Model.Count() > 0)
{
    foreach (var item in Model)
    {
        <a href="Home/LerNotificacao/@item.IDNOTIFICACAO">
            <!--lista de notificações-->
            <i class="fa fa-users text-aqua"></i> @Html.DisplayFor(c => item.DESCRICAO);
        </a>
    }
}

The problem occurs when and called a new listing page because it will also have its own IEnumerable, page call example:

public ActionResult ListarValorAssinatura()
{
    var tbuscar = new ValorAssinaturaAplicacao();
    var listar = tbuscar.ListarTodos();

    var tbuscarNotificacao = new NotificacaoApliacao();
    var retorno = tbuscarNotificacao.ListarTodos();

    if (retorno != null)
    {
        ViewData["QtdNotificacao"] = retorno.Count();
        // ViewData["ListaNotificacao"] = retorno;
    }

    return View(listar);
}

At this point I'm returning the list, but I'm not returning the notifications, so I have an error:

  

An exception of type 'System.InvalidOperationException' occurred in   System.Web.Mvc.dll but was not handled in user code Additional   information: The model item passed into the dictionary is of type   'System.Collections.Generic.List 1[Generico.Dominio.TB_VALOR_ASSINATURA]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 1 [Generico.Domain.TB_NOTIFICACAO]'.

Options I've already tested:

@Html.Partial("_PartialNotificacoes")
@Html.Partial("~/BuscarNotificacoes/Home/_PartialNotificacoes.cshtml")
@Html.Action("BuscarNotificacoes", "Home").
    
asked by anonymous 22.10.2016 / 21:40

1 answer

1

Make a class that will represent the result of the two collections:

public class ViewModelHome
{
    public List<Generico.Dominio.TB_NOTIFICACAO> Notificacoes { get; set; }
    public List<Generico.Dominio.TB_VALOR_ASSINATURA> Assinaturas { get; set; }
}

In ActionResult , create an object of this class ViewModelHome and pass the corresponding values to its properties ( Notificacoes and Assinaturas )

public ActionResult ListarValorAssinatura()
{
    var viewModelHome = new ViewModelHome();

    var tbuscar = new ValorAssinaturaAplicacao();
    viewModelHome.Assinaturas = tbuscar.ListarTodos();

    var tbuscarNotificacao = new NotificacaoApliacao();
    viewModelHome.Notificacoes = tbuscarNotificacao.ListarTodos();

    return View(viewModelHome);
}

in View main change model to the one that is now being sent to View

@model ViewModelHome

the variable Model will have both list as properties and to pass this on PartialView

@Html.Partial("_PartialNotificacoes", Model.Notificacoes) 

foreach of the parent is something like this:

foreach (var item in Model.Assinaturas)
{
    //.......
}

Note : The names may be at your discretion, this is just to illustrate your doubts.     

22.10.2016 / 22:15