Correct View Creation on MVC using Ajax

Asked

Viewed 266 times

3

I have a page that performs maintaining 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 view heavily typed with Orcamento

@model HT.Dominio.Entidade.Orcamento

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

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

To save I’m doing everything via Ajax, filling everything in hand, a damned 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?

Split into Forms in view for each entity and sends through submit, or create in partialview and call each one in his place? Or leave everything as it is and go away?

Thank you, guys!

1 answer

1

The cool and for the good use of MVC’s Binds is creating even typed views. You could create a presentation model (a class in the model) that includes Budget, Orcamentoitem and Historico. ex: "Presentcaoorcamento". This is because before feeding your View you probably consult some basis, so just fill in your Presentname. With this, on the way to the View and on the way back with the POST you will enjoy the validations using your Displaying. Besides, it’s simple to manipulate.

In the model

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

On the 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

In the post

When posting the object Displayingmarking in a POST method that you receive (Presentingthe scoring presentedOmarking). Your object will come mounted.

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

Browser other questions tagged

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