Include 2 Yen in 1 View!

Asked

Viewed 189 times

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 Releases). The code is like this!

(promotion class)

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; }
    }
}

(launch class)

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; }
    }
}

(the View Models of the 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 receiving 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 mistake you’re making 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[Sitejapabrindesddd.MVC.Viewmodels.IndexPromocaoLancamento.Promocaolancamentoviewmodel]'.

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

2 answers

0

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

    @model SiteJapaBrindesDDD.MVC.ViewModels.IndexPromocaoLancamento.PromocaoLancamentoViewModel

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

                @foreach (var item in Model.Promocao)
                {                    
                       ...
                }
    </div>
  • Pablo worked, uhullll, thank you very much Pablo!

0

In your view, you ask for one IEnumerable<>, while you should only request a single object from your Viewmodel (after all, this is what you are associating in your Controller):

Just change the first line of your View:

@model SiteJapaBrindesDDD.MVC.ViewModels.IndexPromocaoLancamento.PromocaoLancamentoViewModel

Browser other questions tagged

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