In Razor ASP I'm trying to get the Id of a database object as soon as it is registered. Here's the Json.
function Registrar() {
var obj = { alcohol: $("#alcoholId").val(), tabaco: $("#tabacoId").val(), otros: $("#otrosId").val()};
//alert($("#codigo").val());
$.getJSON("/AntecedentesMvc/Registrar", obj, function (e) {
if (e != "") {
alert(e);
}
else {
alert("se realizó un registro con exito");
GetHabitoId();
}
});
}
function GetHabitoId() {
$.getJSON("/AntecedentesMvc/GetHabitos", function (o) {
$("#Hid").val();
});
}
Registrar() works fine, it registers the object. and upon success (else) it should call the other funciton GetHabitoID(). It never goes to the controller. but If I edit the function Registrar() like this
function Registrar() {
var obj = { alcohol: $("#alcoholId").val(), tabaco: $("#tabacoId").val(), otros: $("#otrosId").val()};
//alert($("#codigo").val());
$.getJSON("/AntecedentesMvc/Registrar", obj, function (e) {
if (e != "") {
alert(e);
}
else {
alert("se realizó un registro con exito");
}
GetHabitoId();
});
}
function GetHabitoId() {
$.getJSON("/AntecedentesMvc/GetHabitos", function (o) {
$("#Hid").val();
});
}
GetHabitoId() does call the ActionResult. The issue is that it calls it before Registrar() does it's stuff. Returning null...
here are the controllers
Habitos objHabito=new Habitos();
[HttpPost]
public ActionResult Registrar(int alcohol, int tabaco, string otros)
{
objHabito.Alcohol = alcohol;
objHabito.Tabaco = tabaco;
objHabito.Otros = otros;
db.Habitos.Add(objHabito);
db.SaveChanges();
return View();
}
[HttpGet]
public ActionResult GetHabitos ()
{
var Habitoid = objHabito.Id_Habitos;
return Json(Habitoid,JsonRequestBehavior.AllowGet);
}
and if you need to see the button that calls the function
<input type="button" name="RegistrarH" value="Registrar" class="btn btn-default"onclick="Registrar();" data-dismiss="modal"/>
I'm really confused about this issue...