Sending Model to Controller via Ajax

Asked

Viewed 2,016 times

1

I have a strongly typed View with a small form and a javascript function with ajax:

function emitir () {
    $.ajax({
        type: "POST",
        traditional: true,
        url: '@Url.Action("EmissaoRapida", "CND")',
        data: { model: @Model },
        success: function (data) {
            alert(data.mensagem);
        },
        error: function () {
            alert("Erro na tentativa de emissão!");
        }
    });
}

I simply want to send my data model to my controller, the method that will receive the model has the following signature: public Jsonresult Emissaorapida (Emissaocnd model)

However some error happens, using the Firefox debugger I realized that there is something wrong at the time of the change of the model, because the problem is that the project root is not read as when I type in the view. @model Pro_certidao.Areas.Usuarios.Models.Emissaocnd

In error Pro_certidao is of a different color, I believe the problem is there and the message I fear is this: Javascript runtime error: 'Pro_certidao' is not set

Is there any way to fix this or do something similar by sending the complete data model?

2 answers

2


Try to pass the serialized form in the model.

Assuming your form is id formxpto:

function emitir () {
    var model = $("#formxpto").serialize();
    $.ajax({
        type: "POST",
        traditional: true,
        url: '@Url.Action("EmissaoRapida", "CND")',
        data: model,
        success: function (data) {
            alert(data.mensagem);
        },
        error: function () {
            alert("Erro na tentativa de emissão!");
        }
    });
}

Or by jQuery:

$.post("@Url.Action("EmissaoRapida", "CND")", $("#FormFinalizaOrdem").serialize()).done(function (retorno) {}

Your controller must receive the same object as the model, which in this case is Pro_Certidao.Areas.Usuarios.Models.EmissaoCND

If it works, post it to help other people.

EDIT

Your model has to be annotated [Serializable]:

using System;

[Serializable]
public class EmissaoCND
{
      //Atributos...
}

Your controller method annotated with [HttpPost]:

[HttpPost]
public ActionResult EmissaoRapida(EmissaoCND EmissaoCND) {}
  • The error no longer happens, at least the browser does not complain, but when it arrives in the method of my controller the model is null.

  • check if your controller method is annotated with [HttpPost] and if your model is with [Serializable], of System.SerializableAttribute

  • In the controller it was all right already, but in the Model I added the '[Serializable]' and nothing yet, but I can’t put the 'using' correct, in fact it doesn’t even let me add the 'System.Serializableattribute'

  • I edited the reply post, gives a check as your model should be.

  • It’s pretty much the same, both Model and Controller. Only thing different is that I am using EF and use Dataannotation.Schema to better relate things to the database

  • changed the method emitir(), take a look..

Show 1 more comment

0

Example below;

function DeleteUser() {
    var dados = { Id: $("#deleteuser").val() };

    $.ajax(
        {
            type: "POST",
            url: "/Usuarios/Delete",
            data: dados,
            success: function (msg) {                       

            },
            error: function () {

            }
        });
}

Browser other questions tagged

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