Call Partial with Ienumerable within a page that has its Ienumerable Asp.net mvc?

Asked

Viewed 189 times

1

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

@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, Example page call:

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 to the list, but I’m not returning 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.List1[Generico.Dominio.TB_VALOR_ASSINATURA]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[Generic.Dominio.TB_NOTIFICACAO]'.

Options I’ve tried before:

@Html.Partial("_PartialNotificacoes")
@Html.Partial("~/BuscarNotificacoes/Home/_PartialNotificacoes.cshtml")
@Html.Action("BuscarNotificacoes", "Home").
  • you want to resume two list one would be the page that loads and inside it a partialView also loads the other list?

  • 1

    @Virgilionovic, yes,

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 your 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 the model for the one who 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) 

the foreach of the main is something like this:

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

Observing: the names may be at your discretion, this is only to illustrate your doubt.

  • 1

    I’m very grateful for your help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.