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>
Friend, could you explain to me better what you are suggesting?
– Harry
– FernandoNomellini
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?
– Harry
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 ?– FernandoNomellini
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!
– Harry