Display the return of a query with 2 tables in the same View in ASP.NET MVC

Asked

Viewed 1,927 times

1

Controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HelloMobile.Models;

namespace HelloMobile.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        private BANCOTESTEEntities dao = new BANCOTESTEEntities();



        public ActionResult Index()
        {
            try
            {
               var Query =
                from categoria in dao.TB_CATEGORIA
                join subcategoria in dao.TB_SUB_CATEGORIA on categoria.IDCATEGORIA equals subcategoria.IDCATEGORIA 
                select new {idCategoria = categoria.IDCATEGORIA,
                            DesCategoria = categoria.DESCRICAO_CATEGORIA,
                            idSubCategoria = subcategoria.IDSUBCATEGORIA,
                            DesSubCategoria = subcategoria.DESCRICAO_SUBCATEGORIA };

                return View(Query.ToList());
            }
            catch (Exception ex)
            {
                TempData["Erro"] = "Erro na consulta dos dados " + ex.Message;
            }

            return View();
        }





    }
}

I thought I’d do a generic class :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace HelloMobile.Models
{
    public class CLTB_CATEGORIA
    {
        public virtual ICollection<TB_CATEGORIA> TB_CATEGORIAS { get; set; }
        public virtual ICollection<TB_SUB_CATEGORIA> TB_SUB_CATEGORIAS { get;set;}
    }
}

View:

@model List<HelloMobile.Models.CLTB_CATEGORIA>

@{
    ViewBag.Title = "Index";
}


       <div data-role="panel" id="categorias" data-position="left" data-display="reveal" data-theme="a">

            <div data-role="controlgroup" class="ui-icon-alt" > 

            @foreach (var categoria in Model)
             { 
                 <div data-role="collapsible" data-mini="true" data-theme="a" data-content-theme="a">
                    <h1>@categoria.DESCRICAO_CATEGORIA</h1>
                    <ul data-role="listview">
                        @foreach (var subcategoria in Model)
                        { 
                          <li><a href="#perguntas" class="ui-btn ui-mini" data-rel="close" data-transition="fade">@subcategoria.</a></li>  
                        }
                    </ul>
                </div>
             }

            </div>
     </div>

1 answer

1

In your result action, Voce can instantiate a CLTB_CATEGORIA, popular the two properties and send it to the View. Use normally:

Your Controller

var modelo = new TB_CATEGORIAS();
modelo.TB_CATEGORIAS = Query.ToList();
return View(modelo);

Na View:

@foreach (var categoria in Model.TB_CATEGORIAS)
  • Friend, could you explain to me better what you are suggesting?

    1. Your Model must be the class Hellomobile.Models.CLTB_CATEGORIA, and not a list of it. 2. In Controller Voce you must instantiate an object of this class and populate the two properties. 3. Must return this object to the View and 4 the view receives an object of type Hellomobile.Models.CLTB_CATEGORIA and uses it as such (using each of the two properties)
  • Friend, sorry more I can not understand, I am new in Asp.net mvc, you should have noticed that I am consulting 2 different tables, after that I made a class CLTB_CATEGORIA, is added : Icollection<TB_CATEGORIA> How can I play the result of a query in a single table?

  • Your problem is then in the query assembly. You should exemplify what is in the tables and what you want to return. Your query returns a TabelaGrupoSub what you want in return ?

  • According to the sql query I am bringing the data from the category table and sub category where the return is a Tolist(), in your example you ask to play the result in a table, but this is not possible because I have two related tables. Take a look at my question code. Thank you!

Browser other questions tagged

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