1
I have a method in the controller(void) that receives some parameters(3). I am unable to pass to the method. The other times I did, passing to an object there, I have succeeded, with method returning a json. Well, this way is a void method and I don’t have an object as argument, but rather fields as arguments. See below the method and jquery. If there is possibility to work like this too, of course.
Controller: The [Httppost] attribute makes 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’m going to change the button, but just answer me 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 observe, that only stops at break, after the form or the form controls are sent. After the fields are cleared, you enter the break, and if that’s what I’m thinking, the button sends everything and Zera the fields and then the method is already empty or null. This, I believe, is a feature of the button. Right or not?
I did so 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 on the controller it’s 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 you still make a mistake SaveChanges();
That is the mistake:
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
And in the EntityValidationErrors
I pick this up:
{System.Data.Entity.Validation.DbEntityValidationResult}
Inner Exception está null.
I found the bug. I created a Try.. catch and an Exception of the 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 the BD the field is a varchar(14), so you already know what happened. Size larger than supported.
Your solution worked.
– pnet