I have a method in the controller (void) that receives some parameters (3). I can not get into the method. Of the other times I did, passing to an object there, I have succeeded, with method returning a json. Well, this is a void method and I do not have an object as an argument, but rather fields as arguments. See below the method and jquery. If there is a possibility to work that way too, of course.
Controller: Does the [HttpPost] attribute make sense in this case?
[HttpPost]
public void GravaPainelPesquisa(string _cnpj, string _tecnico, string _obs)
{
V99_WEBEntities db = new V99_WEBEntities();
T_LogCadastroPDV logcadastro = new T_LogCadastroPDV();
logcadastro.DE_Tecnico = _tecnico;
logcadastro.DE_Obs = _obs;
db.T_LogCadastroPDV.Add(logcadastro);
db.SaveChanges();
}
My jquery:
function GravaPainelPesquisa() {
var parametros = $('#txtCnpjPesquisa').val() + ',' + $('#txtTecnicoObs').val() + ',' + $('txtObservacao').val();
$.ajax({
url: 'Pesquisa/GravaPainelPesquisa',
datatype: 'json',
contentType: "application/json; charset=utf-8",
type: "POST",
data: JSON.stringify({ }),
success: function (data) {
//Aqui preciso chamar uma função que gera uma mensagem(não é um alert)
},
error: function (error) {
}
})
}
Function that generates the message:
$(function () {
$("#dialogLogPdv").dialog();
});
I'll change the button, but just answer one thing and see if I'm right. It's a Submit button. What happens is that the parameters are coming empty. I put a break and I notice, that's just for no break, after the form or form controls are sent. After the fields are cleared, it goes into the break, and if that's what I'm thinking, the button sends everything and zeroes the fields and then the method is already empty or null. That, I believe, is a feature of the button. Right or not?
I did this and managed to pass the values to the Controller:
var parametros = jQuery.parseJSON(' { "DE_CnpjPDV": "' + $("#txtCnpjPesquisa").val() + '" , "DE_tecnico": "' + $("#txtTecnicoObs").val() +
'" , "DE_Obs": "' + $("#txtObservacao").val() + '"}');
And in the controller it looks like this:
[HttpPost]
public void GravaPainelPesquisa(T_LogCadastroPDV logcadastro)
{
V99_WEBEntities db = new V99_WEBEntities();
logcadastro.DT_Cadastro = DateTime.Now;
db.T_LogCadastroPDV.Add(logcadastro);
db.SaveChanges();
}
But still it still gives error in SaveChanges();
This is the error:
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
And in EntityValidationErrors
I get this:
{System.Data.Entity.Validation.DbEntityValidationResult}
Inner Exception is null.
I discovered the error. I created a try..catch and an exception of type: DbEntityValidationException
and there I saw what the error is. It turns out that in the view, the cnpj field has a mask and in BD the field is a varchar (14), so you already know what happened. Larger size than supported.