Take Model in View with values from the database

Asked

Viewed 615 times

0

I have that 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 take in my view and work with there. This class(Model) will be fed with this code into 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 the values coming from the above, that is, from the database? One more question: All ID’s in this class are Foreign Key. Is it right to represent a Foreign Key as in class or do I have to do a Icollection?

1 answer

1

From what I understand you want to use your class Montaarvoreacao being the Model of your View.

The Tree action of your controller is returning a Jsonresult. When using a Json return to a View you are automatically selecting an object and returning a Jsondata. If you keep 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 Montararvoreacao entity as a View Model, you can change some things in your Controller such as:

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

    return View(monta_arvore);
}

And in your View you’d have to declare something like:

@model MontaArvoreAcao

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

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

  • Luidy, initially I was like this and copied and sent as was. This way I have no problems with anything, except that in the View I need to set up a checkbox and with Jquery is not mounting. Bring the information, but do not assemble the checks as I put in another post. Then, I had the idea to do directly in cshtml to see if I can work.

  • Hello, you got help in your other post on how to set up checkboxes ? If not, send the link so I can see it and try to help you, please. Abs.

  • No, it’s still open. If I can do with jquery it’s all I’d like.

  • Which link to your post?

  • http://answall.com/questions/32100/carregar-dinamicamente-do-banco-e-montar-um-treeview-no-cshtml-n%C3%A3o-estou-consegu

Browser other questions tagged

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