Create a list with a Model and an Int in C#

Asked

Viewed 168 times

0

I have the following class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Dynamic;

namespace Model
{

    class PromocaoQtdeVendidaModel
    {
        private int idPromocaoQtdeVendida;
        private String descricao;
        private List<ProdutoModel> listaProdutos = new List<ProdutoModel>();                    
        private EmpresaModel empresa = new EmpresaModel();
        private DateTime dataCadastro;
        private DateTime dataInicio;
        private DateTime dataFim;
        private int quantidadeMix;

        public DateTime DataFim
        {
            get { return dataFim; }
            set { dataFim = value; }
        }

        public DateTime DataInicio
        {
            get { return dataInicio; }
            set { dataInicio = value; }
        }

        public DateTime DataCadastro
        {
            get { return dataCadastro; }
            set { dataCadastro = value; }
        }

        public EmpresaModel Empresa
        {
            get { return empresa; }
            set { empresa = value; }
        }

        public int IdPromocaoQtdeVendida
        {
            get { return idPromocaoQtdeVendida; }
            set { idPromocaoQtdeVendida = value; }
        }

        public String Descricao
        {
            get { return descricao; }
            set { descricao = value; }
        }

        public List<ProdutoModel> ListaProdutos
        {
            get { return listaProdutos; }
            set { listaProdutos = value; }
        }        
    }
}

and I need the listProduct to have an INT quantityItem I’m trying to do it this way:

    private List<int> quantidadeIte = new List<int>();

    public List<ProdutoModel> ListaProdutos
    {
        get { return listaProdutos; }
        set { 
                listaProdutos.AddRange(quantidadeItem);
                listaProdutos = value; 
            }
    }   

but, is giving the following error: inserir a descrição da imagem aqui

I didn’t want to change my model product, because, I will use this int only in this model.

someone knows how it could do?

  • Young man, can you [dit] your question and be more specific? Your code doesn’t make sense, so explain to us what your problem is and how you think you can solve it, so we can show you how to do it in code.

  • 1

    Lilloh, I recommend reading about the Viewmodel standard: http://www.eduardopires.net.br/2013/08/asp-net-mvc-view-model-pattern-quando-e-como-use/

  • @Arturotemplário saw the link that you gave me, I believe that this meets what I need. I was thinking that I could make a get and a set inside a model (Product) by the model (Promotion) but, by what I researched it is not possible.

  • @LINQ I don’t know how to further my doubt. But, basically I have a PRODUCT MODEL (that you get the product attributes) and I need to create a PROMOTION MODEL within the promotion will have a product list because I need that within the product list has a minimum quantity int... improved?

1 answer

1


You must use a Viewmodel, which is a Model aimed at what will be displayed in the View. In other words, you can create a Model, Promocaoqtdevendidaviewmodel, for example, where it will have two properties: the Promocaoqtdevendidamodel and the amount of items. In your View, you will work with a Promotional Ienumerable.

Example:

 class PromocaoQtdeVendidaViewModel
{
    public int quantidadeIte {get; set;}

    public ProdutoModel Produto {get; set;}
}
  • I wish those numbers were inside the Model?

  • Yes. The Model would be this new class. Your controller will return Promocaoqtdevendidaviewmodel instead of Productomodel, to your View. In Controller, you will create a list of Promocaoqtdevendidaviewmodel, where each one will have a Productomodel object and the amount of items.

Browser other questions tagged

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