jQuery method is not writing to BD

0

I have a routine to write to DB. I get in AJAX's success, shoot a alert , but it does not record. A button that calls the method passes the parameters to the method in the controller and this method writes, with a SaveChanges() .

Note: It does not give any type of error, but it does not enter the controller method. I put break well in the first key and it does not enter, as if it had not called. I put a Alert() in AJAX success and I get to shoot. My button:

str += '<button id="btn_Confirmar" name="btn_Confirmar" onclick=" return GravaPainelPesquisa();">Confirmar</button>';

My jQuery:

function GravaPainelPesquisa() {

    var parametros = {
        _cnpj: $('#txtCnpjPesquisa').val().replace(/[^\d]+/g, ''),
        _tecnico: $('#txtTecnicoObs').val(),
        _obs: $('#txtObservacao').val() 
    }

    $.ajax({

        url: '/Pesquisa/GravaPainelPesquisa',
        datatype: 'json',
        contentType: "application/json; charset=utf-8",
        type: "POST",
        data: JSON.stringify(parametros),
        success: function (data) {

            alert('Testando hoje, 20/06/2014');
        },
        error: function (error) {
        }
    })
}

And my controller

[HttpPost]
        public void GravaPainelPesquisa(string _cnpj, string _tecnico, string _obs)
        {
            using (V99_WEBEntities db = new V99_WEBEntities())
            {
                T_LogCadastroPDV logcadastro = new T_LogCadastroPDV();

                DateTime _datacadastro = new DateTime();
                DateTime? _datacontrole = new DateTime();
                DateTime? _datatransacao = new DateTime();

                var resultado_log = db.T_CRM_StatusPDV
                    .Join(db.T_PDV, t1 => t1.DE_Cnpj, t2 => _cnpj, (t1, t2) => new { t1, t2})
                    .Where(status => status.t1.DE_Cnpj == _cnpj)
                    .Select(i => new { i.t1.DT_ControleV, i.t1.DT_TransacaoV, i.t2.DataCadastro });

                foreach (var dados in resultado_log)
                {
                    _datacadastro = dados.DataCadastro;
                    _datacontrole = dados.DT_ControleV;
                    _datatransacao = dados.DT_TransacaoV;
                }

                try
                {
                    logcadastro.DE_CnpjPDV = _cnpj;
                    logcadastro.DE_Tecnico = _tecnico;
                    logcadastro.DT_Cadastro = _datacadastro;
                    logcadastro.DT_Controle = _datacontrole;
                    logcadastro.DT_Transacao = _datatransacao;
                    logcadastro.DE_Obs = _obs;

                    db.T_LogCadastroPDV.Add(logcadastro);
                    db.SaveChanges();
                }
                catch (DbEntityValidationException ex)
                {
                    string erro = ex.Message;
                }

            }
        }

The problem is that it was the technical field, had the name changed and as it is required in the BD, it did not record. The interesting thing is it did not go into catch. I used IE and I discovered, what I was not getting with Chrome. Now, the code is coming, not the dropdownlist text. This is the same as: $ ('# ddlTecnico'). val (), and the code is coming. will it be because the value is the code and not the text and I'm getting the val ()?

    
asked by anonymous 20.06.2014 / 14:03

1 answer

1

I believe that what can be happening is "Can not bind multiple parameters to the request's web content post api", where it is not possible to send multiple parameters in a post request, this occurs in both Web API and MVC .

Well, the simplest way (for me) to circumvent this, you should treat the parameters as a single object, you can:

Create an object (with the structure of the parameters):

    [HttpPost]
    public void GravaPainelPesquisa([FromBody]Params param)
    {
        var _cnpj = param._cnpj;
        var _tecnico = param._tecnico;
        var _obs = param._obs;

        // restante do seu codigo
    }

    public class Params
    {
        public string _cnpj { get; set; }
        public string _tecnico { get; set; }
        public string _obs { get; set; }
    }

Or use the generic type of C # dynamic:

    [HttpPost]
    public void GravaPainelPesquisa([FromBody]dynamic param)
    {
        var _cnpj = param._cnpj;
        var _tecnico = param._tecnico;
        var _obs = param._obs;

        // restante do seu codigo
    }

The question of not saving in the database can not help, as I do not work with EF.

    
20.06.2014 / 15:31