c# - ASP.NET MVC4 Entity Framework db.SaveChanges() Condition not working -
i working on project in asp.net mvc4 entity framework. here problem, code send different json object depending on if db.savechanges worked or not !
ive been doing debug console , seems false else part of if , never goes in actual condition if condition ok. hard explain easy understand code here goes.
code controller sending true or false json object javascript
[httppost] public actionresult enregistrer_inscription(inscriptionpost inscpost) { noiretorentities db = new noiretorentities(); inscription insc = new inscription() { inscriptionnom = inscpost.nomj, inscriptionprenom = inscpost.prenomj, inscriptiondatenaissance = inscpost.datenaissance, inscriptiondate = datetime.now, inscriptioncategory = inscpost.category, inscriptiondivision = inscpost.division, inscriptionstatut = inscpost.statut, inscriptiontaille = inscpost.taille, inscriptionsexe = inscpost.sexe, inscriptionecole = inscpost.ecole, inscriptionagesept = inscpost.agesept, inscriptionniveauscolaire = inscpost.niveauscolaire, inscriptionadressej = inscpost.adressej, inscriptionvillej = inscpost.villej, inscriptioncodepostalj = inscpost.codepostalj, inscriptioncourrielj = inscpost.courrielj, inscriptiontelephonej = inscpost.telephonej, inscriptionnomp = inscpost.nomp, inscriptionprenomp = inscpost.prenomp, inscriptionadressep = inscpost.adressep, inscriptionvillep = inscpost.villep, inscriptioncodepostalp = inscpost.codepostalp, inscriptioncourrielp = inscpost.courrielp, inscriptiontelephonep = inscpost.telephonep, inscriptionnomm = inscpost.nomm, inscriptionprenomm = inscpost.prenomm, inscriptionadressem = inscpost.adressem, inscriptionvillem = inscpost.villem, inscriptioncodepostalm = inscpost.codepostalm, inscriptioncourrielm = inscpost.courrielm, inscriptiontelephonem = inscpost.telephonem, inscriptionnotes = inscpost.notes, }; try { db.inscription.add(insc); db.savechanges(); if (db.savechanges() > 0) { return json(new { success = true }); } else { return json(new { success = false }); } } catch (system.data.entity.validation.dbentityvalidationexception ex) { return json(new { success = false }); } } }
here jquery code
success: function (data) { console.log(data); console.log(data.success); if (data.success == true) { console.log("succ"); console.log(data); alertify.success("formulaire recu avec succès", 5000); vider_champs() } else { console.log("nope"); console.log(data); alertify.error("le formulaire comporte des erreurs", 5000); } }, error: function () { console.log("error"); alertify.error("le formulaire comporte des erreurs", 5000); } }); }
so problem on code : if (db.savechanges() > 0)
the issue calling db.savechanges()
twice:
db.savechanges(); // save changes database if (db.savechanges() > 0) // save changes once again { return json(new { success = true }); }
first time call method changes saved , there nothing save next time call it. instead, save result of call in variable , use in next line:
int result = db.savechanges(); if (result > 0) { return json(new { success = true }); }
Comments
Post a Comment