jquery method does not pass parameters to controller

1

Jquery does not pass parameters to controller. The values are coming in blank.

Jquery:

function GravaEvento() {    
var str = "";
var parametros = jQuery.parseJSON('{ "DE_Cnpj": "' + $("#txtCnpj").val() + '" , "DE_Descricao": "' + $("#txtDescricao").val() + '" , "DE_UsuProxAcao": "' + $("#txtUsuarioProxAcao").val()
                                    + '" , "DE_UsuProxAcao": "' + $("#txtProxAcao").val() + '" , "DT_ProxAcao": "' + $("#txtDataCadastro").val()
                                    + '" , "ID_TipoProxAcao": "' + $("#txtDataCadastro").val() + '" }');

$.ajax({
    url: '/GerenciarPDV/GravaEvento',
    datatype: 'json',
    contentType: 'applicatio/json; charset=utf-8',
    type: 'POST',
    data: JSON.stringify({ _evento: parametros }),
    success: function (data) {

    },
    error: function () {

    }})
}

Controller

[HttpPost]
public JsonResult GravaEvento(T_CRM_Evento _evento)
{
    V99_WEBEntities db = new V99_WEBEntities();
    T_TecnicoExterno user = new T_TecnicoExterno();
    user = (T_TecnicoExterno)SessaoUtil.Recuperar("sessionUser");
    try
    {
        T_CRM_Evento evento = new T_CRM_Evento();

        evento.DE_Cnpj = _evento.DE_Cnpj;
        evento.DE_Descricao = _evento.DE_Descricao;
        evento.DE_Usuario = user.DE_Login;
        evento.DE_UsuProxAcao = _evento.DE_UsuProxAcao;
        evento.DT_Inclusao = DateTime.Now;
        evento.DT_ProxAcao = _evento.DT_ProxAcao;
        evento.ID_TipoProxAcao = _evento.ID_TipoProxAcao;

        db.T_CRM_Evento.Add(evento);
        db.SaveChanges();
    }
    catch(Exception ex)
    {
        string erro = ex.Message;
    }
    return Json(new { }, JsonRequestBehavior.AllowGet);
}

The function calls the controller, but does not pass the values. T_TecnicoExterno is a BD entity mapped by my Entity. I thought about creating an object with the same fields, but I thought it was repeating what already exists or not?

Jquery

function GravaEvento() {
    var parametros = { _cnpj: $('#txtCnpj').val(), _descricao: $("#txtDescricao").val() };    
    $.ajax({    
        url: '/GerenciarPDV/GravaEvento',
        datatype: 'json',
        contentType: 'applicatio/json; charset=utf-8',
        type: 'POST',
        data: JSON.stringify(parametros),
        success: function (data) {

        },
        error: function () {

        }
    })
}

Controller

[HttpPost]
public void GravaEvento(string _cnpj, string _descricao)
{
    V99_WEBEntities db = new V99_WEBEntities();
    try
    {
        T_CRM_Evento evento = new T_CRM_Evento();

        evento.DE_Cnpj = _cnpj;
        evento.DE_Descricao = _descricao;

        db.T_CRM_Evento.Add(evento);
        db.SaveChanges();
    }
    catch(Exception ex)
    {
        string erro = ex.Message;
    }
}
    
asked by anonymous 04.07.2014 / 00:06

2 answers

2

Use the $. method that is simpler and works perfectly:

Javascript:

function GravaEvento() {
    $.post("/Home/GravaEvento",
        {
            DE_Cnpj: $("#txtCnpj").val(),
            DE_Descricao: $("#txtDescricao").val(),
            DE_UsuProxAcao: $("#txtUsuarioProxAcao").val(),
            ID_TipoProxAcao: $("#txtDataCadastro").val(),
            DT_ProxAcao: $("#txtDataCadastro").val()
        },
       function (data) {

       }, 'json');
} 

Method:

[HttpPost]
public JsonResult GravaEvento(T_CRM_Evento t_crm_evento)
{
    return Json(new { Status = true }, JsonRequestBehavior.DenyGet);
}

Debug:

    
04.07.2014 / 02:38
0

I have an example of a tcc I did once:

[HttpPost]
        public ActionResult CalendarData(FormCollection f)
        {

            string datainicio = Request.Form["data_inicio"];
            string datafinal = Request.Form["data_final"];
            int sala_id = int.Parse(Request.Form["sala_id"]);
            int turno_id = int.Parse(Request.Form["turno_id"]);

            IList<Reserva> tasksList = new List<Reserva>();
            tasksList = ReservaDAO.getEvents(datainicio, datafinal, sala_id, turno_id);


            return Json(tasksList, JsonRequestBehavior.AllowGet);
        }

See if it helps. Do the ajax the first way I sent it.

You can get the fields in C # this way:

        string DE_Cnpj = Request.Form["DE_Cnpj"];
        string DE_Descricao = Request.Form["DE_Descricao"];
        string DE_UsuProxAcao = Request.Form["DE_UsuProxAcao"];
        string DT_ProxAcao = Request.Form["DT_ProxAcao"];
        int ID_TipoProxAcao = int.Parse(Request.Form["ID_TipoProxAcao"]);

Try this:

function GravaEvento() {
    $.ajax({
      url: "/GerenciarPDV/GravaEvento",
      type: "POST",
      data: { "DE_Cnpj": $("#txtCnpj").val(), "DE_Descricao": $("#txtDescricao").val(), "DE_UsuProxAcao": $("#txtUsuarioProxAcao").val(), "DE_UsuProxAcao": $("#txtProxAcao").val(), "DT_ProxAcao": $("#txtDataCadastro").val(), "ID_TipoProxAcao": $("#txtDataCadastro").val() },
      dataType: "json", 
      success: function(data){
            console.log(data); //aparece o retorno no terminal
      }
    });
}

GravaEvento(); //executa a funcao

You can also pass the parameters at once on the date as follows:

data: $("form").serialize();
    
04.07.2014 / 01:09