Method jQuery is not writing in BD

Asked

Viewed 70 times

0

I have a routine to record in BD. With you in AJAX Success, fire a alert, but does not save. A button that calls the method, passes the parameters to the method in the controller and that method saves, with a SaveChanges().

Note: Does not give any kind of error, but does not enter the method in controller. I put a break right in the first key and does not enter, as if not calling. I put a Alert() in the AJAX Success and I can fire. 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 was the technical field, was with the name changed and as it is required in BD, did not write. Interesting is not enter the catch. I used IE and was able to find out, q wasn’t getting it with Chrome. Now, the code is coming and not the dropdownlist text. step this _tecnico: $('#ddlTecnico'). val(), and the code is coming. will be because the value is the code and not text and I’m getting the val()?

  • is MVC or Web API?

  • MVC. Use MVC with Jquery

  • Have you tried testing your endpoint with Fiddler? Why it might be taking place "Multiple actions Were found that match the request". Code Error 500.

  • The problem is that was the technical field, was with the name changed and as it is required in BD, did not write. Interesting is not enter the catch. I used IE and was able to find out, q wasn’t getting it with Chrome. Now, the code is coming and not the dropdownlist text. step this _tecnico: $('#ddlTecnico'). val(), and the code is coming. will be because the value is the code and not text and I’m getting the val()?

1 answer

1


I believe that what may be happening is "Can’t bind Multiple Parameters to the request’s content web api post", where it is not possible to send multiple parameters in a post request, this occurs, both in the Web API how much in the MVC.

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

Create an object(with the parameter structure):

    [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 generic C# Dynamic type:

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

        // restante do seu codigo
    }

Already the question of not being saving in the database I can not help you because I do not work with EF.

Browser other questions tagged

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