How to pass parameter of type object to JSonResult

2

The view always receives the Null object, why? Here is the snippet of the jquery code:

var obj = new Object();
obj.TBCOMPOSICAOID = 0;
obj.DSCOMPOSICAO = "TESTE";
var TBComposicao = JSON.stringify(obj);

$.getJSON("/Composicao/Novo/", { pComposicao: TBComposicao}, function(data){
  var url = "/Composicao/List";
  window.location.href = url;

}).fail(function (jqxhr, textStatus, error) {
  var err = textStatus + ", " + error;
  alert('erro: ' + err.toString());                                    
});

This is the method called by getJson:

Sofar,IhavepassedtheparametersbyString,andfromnowonIhavetopasstheseparametersbyobject,sincetheformisanEmployee/Addressbook:

ModelTBFunctional

publicclassTBFuncionario{publicintTBFUNCIONARIOID{get;set;}publicstringMATRICULA{get;set;}[DisplayFormat(ApplyFormatInEditMode=true,DataFormatString="{0:dd/MM/yyyy}")]
        public DateTime? DTADMISSAO { get; set; }
        public string NMFUNCIONARIO { get; set; }
        public bool STATUS { get; set; }        
        public string CPF { get; set; }
        public virtual ICollection<TBEndereco> TBEndereco { get; set; }
    }

Model TBEndereco

public class TBEndereco
    {
        [Key]
        public int TBENDERECOID { get; set; }

        public int TBFUNCIONARIOID { get; set; }
        public string LOGRADOURO { get; set; }
        public string NUMLOGRADOURO { get; set; }
        public string COMPLEMENTO { get; set; }
        public string BAIRRO { get; set; }
        public string CEP { get; set; }
        [ForeignKey("TBFUNCIONARIOID")]
        public virtual TBFuncionario TBFuncionario { get; set; }
    }

JavaScript snippet that generates the Employee / Addressed populated object:

//ENVIA DADOS VIA AJAX                
                var i, linhasDaTabela = new Array();
                for (i = 0; i < $('.end_logradouro').length; i++) {

                    linhasDaTabela[i] = new Object(); // ESQUECI ESSA LINHA HEHEHE
                    linhasDaTabela[i].TBFUNCIONARIOID = $('.end_funcionarioid')[i].value;
                    linhasDaTabela[i].LOGRADOURO = $('.end_logradouro')[i].value;
                    linhasDaTabela[i].NUMLOGRADOURO = $('.end_numlogradouro')[i].value;
                    linhasDaTabela[i].COMPLEMENTO = $('.end_complemento')[i].value;
                    linhasDaTabela[i].BAIRRO = $('.end_bairro')[i].value;
                    linhasDaTabela[i].CEP = $('.end_cep')[i].value;
                }

                var funci = new Array();
                funci[0] = new Object();
                funci[0].TBFUNCIONARIOID = 0;
                funci[0].NMFUNCIONARIO = $("#NMFUNCIONARIO").val();
                funci[0].MATRICULA = "12312";
                funci[0].DTADMISSAO = $("#DTADMISSAO").val();
                funci[0].STATUS = false;
                funci[0].CPF = "13500305890";
                funci[0].TBEndereco = linhasDaTabela;

                $.ajax({
                    url: 'Novo',
                    datatype: 'json',
                    type: 'post',
                    tBFuncionario: funci,
                    contentType: 'application/json; charset=utf-8',
                    sucess: function () {
                        alert("Sucesso");
                    },
                    error: function (xhr, er) {
                        alert("Ocorreu erro!");
                    }
                });

The Controller that will receive Employee and Address data:

public JsonResult Novo(TBFuncionario tBFuncionario)
        {

            if (ModelState.IsValid)
            {
                //_IRepositorio.InsereFuncionario(tBFuncionario);
                //return RedirectToAction("List");
            }

            return Json(tBFuncionario, JsonRequestBehavior.AllowGet);
        }

The problem is as follows: When I click on the Save button I execute the previously cited Javascript snippet, see the result:

Beautydataisgeneratedasexpected,howeverintheControllertheobjectreturnsempty:

I really do not know what to do, does anyone have any suggestions ??

    
asked by anonymous 11.08.2015 / 16:16

1 answer

1

The most immediate solution I could get was to pass the parameters as text:

var TBCOMPOSICAOID = $("#TBCOMPOSICAOID").val();
                        

var DSCOMPOSICAO = $("#DSCOMPOSICAO").val();

$.getJSON("/Composicao/Novo/"
    , {
        pTBCOMPOSICAOID: TBCOMPOSICAOID,
        pDSCOMPOSICAO: DSCOMPOSICAO
      }
      , function (data) {
          var url = "/Administrativo/Composicao/List/";
          window.location.href = url;
      }).fail(function (jqxhr, textStatus, error) {
          var err = textStatus + ", " + error;
          alert('erro: ' + err.toString());
      });
public JsonResult Novo(int pTBCOMPOSICAOID, string pDSCOMPOSICAO)
{
    var model = new TBComposicao
    {
        TBCOMPOSICAOID = pTBCOMPOSICAOID,
        DSCOMPOSICAO = pDSCOMPOSICAO
    };

    _IRepositorio.AtualizarComposicao(model);
    return Json(model, JsonRequestBehavior.AllowGet);
}  

Thank you!

    
13.08.2015 / 14:08