Correct view creation in MVC using Ajax

3

I have a page that maintains a budget.

Below I present a briefing of entities

public partial class Orcamento
{
    public int IdPedidoCompra { get; set; }
    public int IdCliente { get; set; }
    public System.DateTime DataPedido { get; set; }
    public decimal Valor { get; set; }
}

public partial class OrcamentoItem
{
    public int IdOrcamentoItem { get; set; }
    public int IdOrcamento { get; set; }
    public int IdProduto { get; set; }
    public int Quantidade { get; set; }
    public decimal PrecoUnitario { get; set; }
}

public partial class Historico
{
    public int IdHistorico { get; set; }
    public Nullable<int> IdOrcamento { get; set; }
    public string NomeContato { get; set; }
    public System.DateTime DataContato { get; set; }
    public string Observacao { get; set; }
    public Nullable<System.DateTime> DataProximoContato { get; set; }
}

Based on these tables I created my strongly typed view with Orcamento

@model HT.Dominio.Entidade.Orcamento

And the fields of OrcamentoItem and Historico I put in the hand, that is, without using Html.Helpers .

With this I lose the validations that MVC already creates for me when using Helper ValidationMessageFor , among other problems as well.

To save I'm doing everything via Ajax, filling everything in hand, a damn job.

    //cria o objeto json
    var orcamento = {"campo1", "campo2"....}
    var orcamentoitens = {"campo1", "campo2"....}
    //preenche o objeto

    //envia ao servidor
    $.ajax({
        type: "POST",
        url: "/Orcamento/Salvar/",
        data: JSON.stringify(orcamento),
        contentType: 'application/json;',
        datatype: "json",
        success: function (retorno) {}
    });

So I ask, what's the right way to do this?

Separate in forms in view for each entity and send via submit , or create in partialview and call each in your place? Or do I leave everything as is and leave?

Thanks, guys!

    
asked by anonymous 23.06.2015 / 21:58

1 answer

1

The legal and good use of MVC binds is creating really typed views. You could create a presentation template (a class in the model) that encompasses Budget, Budget, and History. eg: "PresentationOrcamento". That's because before you feed your View you probably see some basis, so just fill out your PresentationBox. With this, on the way to the View and back with POST you will enjoy the validations using your PresentationOrcamento. In addition, it's easy to manipulate.

No model

public class ApresentacaoOrcamento
{
Orcamento MeuOrcamento;
List<OrcamentoItem> MeuOrcamentoItem;
Historico MeuOrcamentoHistorico;
}

No controller

public ActionResult SeuMetodoQueChamaAView()
{
var apresentacao = new ApresentacaoOrcamento();
apresentacao.MeuOrcamento = meuRepositorio.GetOrcamentoById(1);
apresentacao.MeuOrcamentoItem = meuRepositorio.GetItensByOrcamentoId(1).toList();
apresentacao.MeuOrcamentoHistorico = meuRepositorio.GetHistoricoByOrcamentoId(1);
return View(apresentacao);
}

Na View

@model HT.Models.ApresentacaoOrcamento

No POST

When you post the PresentationObject object to a POST method that receives (PresentationObject PresentationObject). Your object will come mounted.

This is the way I have almost always done in my implementations. Sometimes it happens to have to manipulate more the object to be sent Via post when I have Bind problems.

    
08.12.2015 / 22:20