简体   繁体   中英

Controller do not receive values passed from json.stringify(obj)

I am not understand this case:

I have a model like:

public class ExmDescobertos {
    public int Id { get; set; }
    public int ExameId { get; set; }
    public int PlanoId { get; set; }
    public int ConvenioId { get; set; }
}

And create an object javascript :

var objDescoberto = new Object();
objDescoberto.Id = $("#hdnDescobertoId").val(); //inputs with values...
objDescoberto.ExameId = $('#hdnExameId').val();
objDescoberto.PlanoId = $('#hdnPlanoId').val();
objDescoberto.ConvenioId = $('#hdnConvenioId').val();

And I am using Json.stringify(obj) to transmit the values with a $.post jQuery method:

var dados = JSON.stringify(objDescoberto);

In this point, dados is "{"Id":"27","ExameId":"53","PlanoId":"32","ConvenioId":"11"}" , for example.

And have a controller with this action:

public PartialViewResult(ExmDescobertos descoberto) { }

But... the parameter in this controller not receive your values correct! :o In this point descoberto is Id = 0; ExameId = 0; PlanoId = 0; ConvenioId = 0;

Not errors explicit, but not works... Anybody have a idea of what I have missing? Thank you for all!

Don't stringify you object, just send object as is.

$.post("/url", objDescoberto);

or

var dados = JSON.stringify({descoberto : objDescoberto});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM