Ajax sending model to controller

0

I would like to know if it is possible for me to pass a "Model" object via ajax to my controller.

        $.ajax({
            type: "POST",
            url: "@Url.Action("CadastrarSementesVariedades", "SementesLevantamentoVariedades")",
            data: model,
        dataType: "html"
    });

I did it this way but it did not work, would I have another way?

    
asked by anonymous 07.03.2017 / 22:11

1 answer

1
var obj = {id: $("#id").val(), descricao: $("#descricao").val()}

$.ajax({
        type: "POST",
        url: "@Url.Action("CadastrarSementesVariedades", "SementesLevantamentoVariedades")",
        data: obj});

public Class Objeto{
   public string id {get; set;}
   public string descricao {get; set;}
}

[httpPost]
public ActionResult CadastrarSementesVariedades(Objeto obj){
   //...
}

Depending on how you created your form, you can also serialize and send direct: date: $ ("# frm"). serialize ()

    
08.03.2017 / 12:09