Return List in a View

1

Hello, I'm a beginner in C # and MVC and am having difficulty returning a list of data. I have the code below and I do not know how to return the data loaded from the list. Can anyone suggest me how to do this? Thanks

  public class EstoqueAcabadoController : Controller
    {
        // GET: EstoqueAcabado
        public ActionResult Index()
        {

            using (BD context = new BD())
            {
                var query = from c in context.V500_ESTOQUE_ACAB select c;
                foreach (var item in query)
                {
                    ViewBag.cod_deposito = item.COD_DEPOSITO;
                    ViewBag.cod_reduzido = item.COD_REDUZIDO;
                    ViewBag.desc_cor =     item.DESC_COR;
                    ViewBag.desc_artigo =  item.DESC_ARTIGO;
                    ViewBag.peso_peca =    item.PESO_PECA;
                }
                return ();
            }
    
asked by anonymous 30.12.2017 / 02:05

1 answer

1

Old I did not quite understand what object you want to pass as list, but what I did there was:

I created a list of V500_ESTOQUE_ACAB

List<V500_ESTOQUE_ACAB> estoque = new List<V500_ESTOQUE_ACAB>();

Then I continued the iteration of the objects within the query and added the objects to the list:

estoque.add(new estoque(){
                COD_DEPOSITO = item.COD_DEPOSITO,
                COD_REDUZIDO = item.COD_REDUZIDO,
                DESC_COR = item.DESC_COR,
                DESC_ARTIGO = item.DESC_ARTIGO,
                PESO_PECA = item.PESO_PECA
            });

And then I added the List<V500_ESTOQUE_ACAB> to ViewBag:

public class EstoqueAcabadoController : Controller
{
    // GET: EstoqueAcabado
    public ActionResult Index()
    {

        List<V500_ESTOQUE_ACAB> estoque = new List<V500_ESTOQUE_ACAB>();
        using (BD context = new BD())
        {
            var query = from c in context.V500_ESTOQUE_ACAB select c;
            foreach (var item in query)
            {
                estoque.add(new estoque(){
                    COD_DEPOSITO = item.COD_DEPOSITO,
                    COD_REDUZIDO = item.COD_REDUZIDO,
                    DESC_COR = item.DESC_COR,
                    DESC_ARTIGO = item.DESC_ARTIGO,
                    PESO_PECA = item.PESO_PECA
                });
            }
            ViewBag.estoque = estoque;
        }

        return View();
    }
}

Within your View you will import this object like this:

List<V500_ESTOQUE_ACAB> estoque = (List<V500_ESTOQUE_ACAB>)ViewBag.estoque;

Edit: If THAT IS NOT THE OBJECTIVE OF THE QUESTION, COMMENT THAT I ADJUST AND THE PEOPLE RESOLVE THAT

    
30.12.2017 / 02:48