0
When I select a specific field from a form, I trigger a request to load some essential information to complete the form, only when selecting a form field it fires 3, 4 requests and this causes my data from an option for example to be duplicated within the form.
// requisição
$('body').on('click', '#cmbContrato', function () {
contrato = $("#cmbContrato").val();
$.ajax({
type: 'GET',
url: 'ProtocoloExterno/GetItensContrato',
data: { dado: contrato },
dataType: 'JSON',
success: function (dados) {
if (dados !== 0) {
var selectbox = $('#cmbItensContrato');
$.each(dados, function (i, d) {
$('<option>').val(d.id).text(d.value).appendTo(selectbox);
});
}
}
},
error: function () {
console.log("Erro ao enviar AJAX");
}
});
});
// metodo em c# asp.net mvc
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public JsonResult GetItensContrato()
{
List<object> resultado = new List<object>();
long numeroContrato = 0;
if (Request.QueryString["dado"] != "")
{
numeroContrato = long.Parse(Request.QueryString["dado"]);
}
var infosContrato = db.ItemContrato.Where(c => c.ContratoId == numeroContrato.ToString()).Select(c => c.ItemRCid);
foreach(int itensContrato in infosContrato)
{
resultado.Add(new { id = itensContrato, value = itensContrato });
}
return Json(resultado, JsonRequestBehavior.AllowGet);
}
How to solve this problem?
managed to solve?
– Leandro Angelo
I managed to solve yes, but it was the outside call... but it keeps coming replicated @Leandroangelo
– gabrielfalieri
In json do they come duplicated? or are they just adding to the ones already on the screen? you added the
remove()
of my edited reply?– Leandro Angelo
in Json they come duplicated, the error is in the return of Entity
– gabrielfalieri
You say duplicated, because it’s passing the same value to the
id
and thevalue
? In this passage:resultado.Add(new { id = itensContrato, value = itensContrato });
– Leandro Angelo
No, that the return that comes from Entity sends twice the same record
– gabrielfalieri
Have you ever made the same query at the bank? Did you actually have duplicate records there?
– Leandro Angelo
No bro, I already did yes, it is return stick of the Entity json
– gabrielfalieri