c# – Correct creation of View in MVC using Ajax


I have a page that maintains a budget.

Below I present a briefing of the 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 my hand, that is, without using Html.Helpers .

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

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

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

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

So I ask: What is the correct way to do this?

I separate in forms in the view for each entity and send it through submit , or do I create in partialview and call each one in its place? Or leave everything as it is and leave?

Thank you guys!


The cool thing and for the good use of MVC binds is creating typed views. You could create a presentation model (a class in the model) that encompasses Budget, Item Budget, and History. ex: "Presentation Budget". That's because before feeding your View you probably consult some base, so just fill in yourPresentationOrcamento. With that, on the way to the View and back with the POST, you will enjoy the validations using your PresentationOrcamento. Furthermore, its manipulation is simple.

in the 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);

In View

@model HT.Models.ApresentacaoOrcamento


By posting the BudgetPresentation object in a POST method that it receives (BudgetPresentation). Your object will come assembled.

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

Scroll to Top