Include 2 IEnumerable in 1 View!

1

Hello, I'm new here and I'm having problems with my application! I want to list 2 list in a view (Promotions and Postings). The code is like this!

(class Promoting)

namespace SiteJapaBrindesDDD.MVC.Entidades
{
    public class CadPromocao
    {
        public int PromocaoId { get; set; }
        public string Codigo { get; set; }
        public string NomeBrinde { get; set; }
        public string Descricao { get; set; }
        public string Foto { get; set; }
    }
}

(class Release)

namespace SiteJapaBrindesDDD.MVC.Entidades
{
    public class CadLancamento
    {
        public int LancamentoId { get; set; }
        public string Codigo { get; set; }
        public string NomeBrinde { get; set; }
        public string Descricao { get; set; }
        public string Foto { get; set; }
    }
}

(View Models of Classes)

namespace SiteJapaBrindesDDD.MVC.ViewModels
{
    public class CadLancamentoViewModel
    {
        [Key]
        public int LancamentoId { get; set; }
        public string Codigo { get; set; }
        public string NomeBrinde { get; set; }
        public string Descricao { get; set; }
        public string Foto { get; set; }

        public HttpPostedFileBase File { get; set; }
    }
}

namespace SiteJapaBrindesDDD.MVC.ViewModels
{
    public class CadPromocaoViewModel
    {
        [Key]
        public int PromocaoId { get; set; }
        public string Codigo { get; set; }
        public string NomeBrinde { get; set; }
        public string Descricao { get; set; }
        public string Foto { get; set; }

        public HttpPostedFileBase File { get; set; }
    }
}

(View Model that receives the 2 classes)

namespace SiteJapaBrindesDDD.MVC.ViewModels.IndexPromocaoLancamento
{
    public class PromocaoLancamentoViewModel
    {
        private IEnumerable<CadLancamentoViewModel> lancamento;
        private IEnumerable<CadPromocaoViewModel> promocao;

        public PromocaoLancamentoViewModel(IEnumerable<CadLancamentoViewModel> lancamento, IEnumerable<CadPromocaoViewModel> promocao)
        {
            this.lancamento = lancamento;
            this.promocao = promocao;
        }

        public IEnumerable<CadLancamento> Lancamento { get; set; }
        public IEnumerable<CadPromocao> Promocao { get; set; }
    }
}

(Controller)

public ActionResult Index()
        {
            var promo = Db.CadPromocaos.ToList();
            var promocao = Mapper.Map<IEnumerable<CadPromocao>, IEnumerable<CadPromocaoViewModel>>(promo);

            var lanc = Db.CadLancamentos.ToList();
            var lancamento = Mapper.Map<IEnumerable<CadLancamento>, IEnumerable<CadLancamentoViewModel>>(lanc);

            var promocaoLancamento = new PromocaoLancamentoViewModel(lancamento, promocao);

            return View(promocaoLancamento);
        }

(my VIEW Index)

@model IEnumerable<SiteJapaBrindesDDD.MVC.ViewModels.IndexPromocaoLancamento.PromocaoLancamentoViewModel>

<div class="container body-content">

            @foreach (var item in Model.GetEnumerator().Current.Promocao)
            {
                <div id="conteudo_promocao">
                    <div id="caixa_promocao">
                        <img width="100%" height="250" src="@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ })" />
                        <h3>Código: @item.Codigo</h3>
                        <h4>@item.Descricao</h4>
                        <div id="link_caixa">
                            <p>@Html.ActionLink("Pedir Orçamento", "Orcamento", new { /* id=item.PrimaryKey */ })</p>
                        </div>
                    </div>
                </div>
            }
        </div>

(Now the error you are giving is this)

  

The model item passed into the dictionary is of type   'SiteJapaBrindesDDD.MVC.ViewModels.IndexPromocaoLancamento.PromocaoLancamentoViewModel',   but this dictionary requires a model item of type   'System.Collections.Generic.IEnumerable'1 [SiteDownloadsDDD.MVC.ViewModels.IndexPromoteLancamento.PromocaoLancamentoViewModel]'.

What I do, I'm 3 days trying to take the 2 list to the view and I can not

    
asked by anonymous 08.03.2017 / 14:22

2 answers

0

In your view, you just have to change your model to use your PromocaoLancamentoViewModel class

    @model SiteJapaBrindesDDD.MVC.ViewModels.IndexPromocaoLancamento.PromocaoLancamentoViewModel

    <div class="container body-content">    
                @foreach (var item in Model.Lancamento)
                {                    
                       ...
                }

                @foreach (var item in Model.Promocao)
                {                    
                       ...
                }
    </div>
    
08.03.2017 / 14:29
0

In your view, you ask for a IEnumerable<> , whereas you should only request a single object from your ViewModel (after all, that's what you're associating with your Controller):

Just change the first line of your View:

@model SiteJapaBrindesDDD.MVC.ViewModels.IndexPromocaoLancamento.PromocaoLancamentoViewModel
    
08.03.2017 / 14:30