J'utilise ajax pour appeler une vue partielle avec une table à l'intérieur d'un div appelé "div-GVPrevision". J'utilise des tables de données, mais j'obtiens une erreur après l'appel ajax, et cela dit:
"Avertissement DataTables: table id = GVPrevision - Impossible de réinitialiser DataTable. Pour plus d'informations sur cette erreur, veuillez consulter http: // datatables.net/tn/3 "
Voici le code ajax:
<script>
function GVPrevision() {
$('#GVPrevision').DataTable({
"aaSorting": [],
aLengthMenu: [
[4, -1],
[4, "Todo"]
],
responsive: false
});
}
$(document).ready(function () {
GVPrevision();
$('.btnagregar').click(function (event) {
event.preventDefault();
var data = {
"codmov": $("#codmovf").val(),
"fechainicio": $("#fechainiciof").val(),
"fechatermino": $("#fechaterminof").val(),
"rutentidad": $("#rutentidadf").val(),
"motivo": $("#motivof").val()
};
$.ajax({
url: "/Ficha/AgregarfooterPrevision",
type: "POST",
data: JSON.stringify(data),
dataType: "json",
contentType: "application/json",
success: function (response) {
if (response.Success) {
$("#div-GVPrevision").load('@Url.Content("~/Ficha/GVPrevision")');
GVPrevision();
}
else
window.location.href = "@Url.Action("Prevision", "Ficha")";
},
error: function () {
console.log('Login Fail!!!');
}
});
});
});
</script>
2 réponses
J'ai résolu le problème en remplaçant le code par la méthode jquery $ .post ():
if (response.Success) {
$.post("/Ficha/GVPrevision", function (datos) {
$("#div-GVPrevision").html(datos);
GVPrevision();
})
}
Comme je crois que la base de données est initialisée plus d'une fois puisque vous ne montrez pas tout le code, je ne peux que vous fournir cette solution simple mais pas du tout recommandée est de détruire la table de données, puis d'appeler à nouveau le GCPervision, ce n'est pas du tout recommandé, mais est une solution, utilisez simplement la fonction Distroy
function GVPrevision() {
$('#GVPrevision').DataTable({
"aaSorting": [],
aLengthMenu: [
[4, -1],
[4, "Todo"]
],
responsive: false
});
}
$(document).ready(function () {
GVPrevision();
$('.btnagregar').click(function (event) {
event.preventDefault();
var data = {
"codmov": $("#codmovf").val(),
"fechainicio": $("#fechainiciof").val(),
"fechatermino": $("#fechaterminof").val(),
"rutentidad": $("#rutentidadf").val(),
"motivo": $("#motivof").val()
};
$.ajax({
url: "/Ficha/AgregarfooterPrevision",
type: "POST",
data: JSON.stringify(data),
dataType: "json",
contentType: "application/json",
success: function (response) {
if (response.Success) {
$('#GVPrevision').DataTable().destroy();
$("#div-
GVPrevision").load('@Url.Content("~/Ficha/GVPrevision")');
GVPrevision();
}
else
window.location.href = "@Url.Action("Prevision", "Ficha")";
},
error: function () {
console.log('Login Fail!!!');
}
});
});
});
</script>
Questions connexes
De nouvelles questions
c#
C # (prononcé "see sharp") est un langage de programmation multi-paradigme de haut niveau, typé statiquement développé par Microsoft. Le code C # cible généralement la famille d'outils et d'exécutions Microsoft .NET, notamment le .NET Framework, .NET Core et Xamarin. Utilisez cette balise pour les questions sur le code écrit en C # ou en spécification formelle de C #.