Return List in a View

Asked

Viewed 308 times

1

Hello, I’m a beginner in C# and MVC and I’m having trouble returning a data list. I have the code below and do not know how to return the loaded data from the list. Can anyone suggest me how to do this? Thank you

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

1 answer

2

Old I didn’t quite understand which 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 inside the query and added the objects in 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> in 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();
    }
}

Inside 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 PURPOSE OF THE QUESTION, COMMENTS THAT I ADJUST AND WE RESOLVE IT

  • In fact, I’m learning the MVC. So I made a way to get a column just from my table, to play there in the view and it worked. Now, I want to take all the columns of my table (which is the query object) to play there in the view. Get it ? I think it’s a list right, I need to do. Thank you!!

  • Ahhh Cleiton, you want to kind of put everything in one place ?

  • @Cleitondelims I recommend you create an object with the variables you want and pass the object as a whole. That would be it ?

  • I want for example to "load" the result of my select (with all columns) to show everything in the view, understand?

  • @Cleitondelimas OK got it, explain something to me... V500_ESTOQUE_ACAB is an object with attributes: COD_DEPOSITO, COD_REDUZIDO,DESC_COR,DESC_ARTIGO and PESO_PECA right

  • In fact, v500_estoque_acab is the table of my database, and it has 30 columns. What I want is to bring all the columns of this table, and play as the controller’s Return, to be able to access my view and list the result of the query.

Show 1 more comment

Browser other questions tagged

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