I made a code using Linq and jQuery . It turns out that it is giving me a problem that I can not solve, which is to dynamically create a Treeview . Then I had another idea. Make foreach
direct in View (cshtml). Here's the question, how do I foreach
within View and over a result of a Linq that is in my controller?
Controller :
[HttpPost]
public JsonResult ArvoreAcao(string _uf)
{
RupturaEntities db = new RupturaEntities();
var monta_arvore = (from rup in db.Ruptura
from apr in db.Apresentacao
.Where(apr => apr.Codigo_Apresentacao == rup.Codigo_Apresentacao)
from pdv in db.PDV.Where(pdv => pdv.CodigoPDV == rup.CodigoPDV)
from mot in db.Motivo.Where(mot => mot.IDMotivo == rup.IDMotivo)
select new {
rup.IDRuptura,
rup.DataRuptura,
rup.IDMotivo,
mot.Motivo1,
rup.IDOrigem,
rup.CodigoPDV,
pdv.UF,
pdv.Cidade,
loja = pdv.Cnpj + " - " + pdv.Descricao,
rup.Codigo_Apresentacao,
apr.Unidade_Negocio,
apr.Franquia,
apr.Familia,
apr.Descricao
}).ToList().Distinct().OrderBy(apr => apr.Descricao);
return Json(new { monta_arvore }, JsonRequestBehavior.AllowGet);
}
And that would be View where foreach
will happen. The foreach
would be inside div jqxTree , ie it would erase all static content from there (fake) and do it dynamically.
@{
ViewBag.Title = "Acao";
Layout = "~/Views/Shared/_LayoutBase.cshtml";
}
<link href="~/Content/Jqwidgets/jqx.base.css" rel="stylesheet" type="text/css" />
<h2>Tomada de Ação</h2>
<div class="row">
<div class="col-md-12">
<div class="col-md-1">
<label for="cbxCodTipo">UF:</label>
</div>
<div class="col-md-4">
<select class="form-control col-md-6" name="cbxCodTipo" id="cbxCodTipo"
onchange=" return MontaCidades();">
<option value="00">Selecione um estado</option>
<option value="AC">ACRE</option>
<option value="AL">ALAGOAS</option>
<option value="AP">AMAPÁ</option>
<option value="AM">AMAZONAS</option>
<option value="BA">BAHIA</option>
<option value="CE">CEARÁ</option>
<option value="DF">DISTRITO FEDERAL</option>
<option value="ES">ESPÍRITO SANTO</option>
<option value="GO">GOIÁS</option>
<option value="MA">MARANHÃO</option>
<option value="MT">MATO GROSSO</option>
<option value="MS">MATO GROSSO DO SUL</option>
<option value="MG">MINAS GERAIS</option>
<option value="PA">PARÁ</option>
<option value="PB">PARAÍBA</option>
<option value="PR">PARANÁ</option>
<option value="PE">PERNAMBUCO</option>
<option value="PI">PIAUÍ</option>
<option value="RJ">RIO DE JANEIRO</option>
<option value="RN">RIO GRANDE DO NORTE</option>
<option value="RS">RIO GRANDE DO SUL</option>
<option value="RO">RONDÔNIA</option>
<option value="RR">RORAIMA</option>
<option value="SC">SANTA CATARINA</option>
<option value="SP">SÃO PAULO</option>
<option value="SE">SERGIPE</option>
<option value="TO">TOCANTINS</option>
</select>
</div>
<div class="col-md-6">
<div class="col-md-2">
<label for="cbxCidade">Cidade:</label>
</div>
<select class="form-control col-md-4" name="cbxCidade" id="cbxCidade">
</select>
</div>
</div>
<div class="col-md-12">
<div class="col-md-1">
<label for="cbxRede">Rede:</label>
</div>
<div class="col-md-4">
<select class="form-control col-md-6" name="cbxRede" id="cbxRede"></select>
</div>
<div class="col-md-6">
<div class="col-md-2">
<label for="cbxRede">Descrição:</label>
</div>
<select class="form-control col-md-4" name="cbxDescricao" id="cbxDescricao">
</select>
</div>
</div>
<div class="col-md-12">
<div class="col-md-1">
<label for="cbxProduto">Produto:</label>
</div>
<div class="col-md-4">
<select class="form-control col-md-6" name="cbxProduto" id="cbxProduto">
</select>
</div>
</div>
<div class="col-md-12">
<div class="col-md-1">
<label for="cbxUnNegocio">Unidade Negócio:</label>
</div>
<div class="col-md-4">
<select class="form-control col-md-6" name="cbxUnNegocio" id="cbxUnNegocio">
</select>
</div>
</div>
</div>
<br />
<div id="content">
<div class="listTree"></div>
<button class="btn btn-primary" onclick=" return MontaArvore();">Pesquisar</button>
</div>
<br>
<div id='jqxWidget'>
<div style='float: left; width:auto;'>
<div id='jqxTree' style='visibility: hidden; float: left; margin-left: 20px;'>
<ul>
<li item-checked='false' item-expanded='false'>
Produto
<ul>
<li item-expanded='true'>
MIP
<ul>
<li item-expanded='true'>
Família: ACCUVIT
<ul>
<li>ACCUVIT COMREV FRX30</li>
</ul>
</li>
<li item-expanded='true'>
Família: FLOGORAL
<ul>
<li>FLOGORAL SPRAY CEREJA CTX30ML</li>
<li>FLOGORAL SPRAY MENTA CTX30ML</li>
<li>FLOGORAL CREM DENTAL CTX70G</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<div style='margin-left: 60px; float: left;'>
<div style='margin-top: 10px;'>
<input id='jqxCheckBox' type="hidden">
</div>
</div>
</div>
</div>@*Fim da div jqxWidget *@
<div class="row">
<div class="col-md-12">
<div class="col-md-2">
<label for="txtObs">Observação:</label>
</div>
<div class="col-md-12">
<textarea id="txtObs" style="width: 450px;"></textarea>
</div>
</div>
</div>
<br />
<div id="content">
<div class="listTree"></div>
<button class="btn btn-success" ">Gravar</button>
@Html.ActionLink("Voltar", "Index", "Home", new {}, new {@class = "btn btn-danger"})
</div>
@*<script class="cssdeck"
src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>*@
<script class="cssdeck"
src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.1/js/bootstrap.min.js">
</script>
<script src="~/Scripts/Acao/Acao.js"></script>
<script src="~/Scripts/Jqwidgets/jqxcore.js"></script>
<script src="~/Scripts/Jqwidgets/jqxbuttons.js"></script>
<script src="~/Scripts/Jqwidgets/jqxscrollbar.js"></script>
<script src="~/Scripts/Jqwidgets/jqxpanel.js"></script>
<script src="~/Scripts/Jqwidgets/jqxtree.js"></script>
<script src="~/Scripts/Jqwidgets/jqxcheckbox.js"></script>
Important : The controller is called AcaoController
. .edmx is within the Model (Folder) and Entity usage.
I did this in the controller :
ViewBag.result_arvore = monta_arvore;
And then this in View .
@foreach (var item in ViewBag.result_arvore)
{
<ul>
<li item-checked='false' item-expanded='false'>
@item.Motivo1
</li>
</ul>
}
The result is this:
HowdoImakeaforeach
,butontopoftheresultofalinqcomingfromthecontroller?
Imadechangesanditgivesmetheerrorthatforeach
cannotbedone,becausemyclassdoesnothaveapublicGetEnumeratorproperty.HowdoIdoit?
Itlookslikethis:MyViewisonlytheforeach
part.Therestarethesameasabove.
<divid='jqxWidget'><divstyle='float:left;width:auto;'><divid='jqxTree'style='visibility:hidden;float:left;margin-left:20px;'>@foreach(variteminModel){<ul><liitem-checked='false'item-expanded='false'>@item.Motivo</li></ul>}</div><divstyle='margin-left:60px;float:left;'><divstyle='margin-top:10px;'><inputid='jqxCheckBox'type="hidden">
</div>
</div>
</div>
</div>
The code in my controller :
public ActionResult Acao()
{
RupturaEntities db = new RupturaEntities();
var monta_arvore = db.Ruptura.Select(rup=> new MontaArvoreAcao {
IDRuptura = rup.IDRuptura,
DataRuptura = rup.DataRuptura,
IDMotivo = rup.IDMotivo,
Motivo = rup.Motivo.Motivo1,
IDOrigem = rup.IDOrigem,
CodigoPDV = rup.CodigoPDV,
UF = rup.PDV.UF,
Cidade = rup.PDV.Cidade,
CnpjDescricao= rup.PDV.Cnpj + " - " + rup.PDV.Descricao,
Codigo_Apresentacao = rup.Codigo_Apresentacao,
Unidade_Negocio = rup.Apresentacao.Unidade_Negocio,
Franquia = rup.Apresentacao.Franquia,
Familia = rup.Apresentacao.Familia,
Descricao = rup.Apresentacao.Descricao
}).ToList();
return View(monta_arvore);
}
My Model :
public class MontaArvoreAcao
{
public int IDRuptura { get; set; }
public DateTime DataRuptura { get; set; }
public int IDMotivo { get; set; }
public string Motivo { get; set; }
public int IDOrigem { get; set; }
public string CodigoPDV { get; set; }
public string UF { get; set; }
public string Cidade { get; set; }
public string CnpjDescricao { get; set; }
public string Codigo_Apresentacao { get; set; }
public string Unidade_Negocio { get; set; }
public string Franquia { get; set; }
public string Familia { get; set; }
public string Descricao { get; set; }
}
public class MontaArvoreAcaoDBContext : DbContext
{
public DbSet<MontaArvoreAcao> montaArvoreAcao { get; set; }
}