Get Model in the View with values coming from the database

0

I have this class:

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 Cnpj { get; set; }
    public string Descricao { 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; }
}

The purpose of this class is for me to have a Model that I can pick up from my view and work with it there. This class (Model) will be fed with this code in the 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);

    ViewBag.result_arvore = monta_arvore;    
    return Json(new { monta_arvore}, JsonRequestBehavior.AllowGet);
}

The question is: How do I get this Model in the View with all values coming from linq above, ie from the database? One more question: All IDs of this class are Foreign Key. Is it correct how to represent a Foreign Key as it is in the class or do I have to do an ICollection?

    
asked by anonymous 15.09.2014 / 13:37

1 answer

1

So I understand that you want to use your MontaZirAction class as the Model of your View.

The Action Action of your controller is returning a JsonResult. When using a Json return to a View you are serializing an object and returning a JsonData. If you continue doing the way you are proposing above you will have to treat via javascript / JQuery the return to your View and then you would have something like:

$.ajax({
  type: "POST",
  url: "SuaController/ArvoreAcao",
  data: { _uf : seuCampoUf },
  success: function (montaArvore) {
         // Aqui utiliza sua entidade e coloca os valores nos 
         // campos/tags html que deseja utilizando por exemplo montaArvore.Descricao
}); 

If your goal is to use the MountMouse entity as a View Model, you can change some things on your Controller as:

[HttpPost]
public ActionResult ArvoreAcao(string _uf)
{
    // Faz o que é necessário

    return View(monta_arvore);
}

And in your View you would have to declare something like:

@model MontaArvoreAcao

In this case you could use the Model within the View.

There is another detail that you are putting the monta_arvore entity into a ViewBag. In that case you could also use your ViewBag within the View.

    
15.09.2014 / 14:20