-1

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...

Udara Kasun
  • 2,182
  • 18
  • 25
  • Try public JsonResult GetHabitos() – Azar Shaikh Sep 25 '18 at 02:53
  • Why you're using `return View();` if you want to return back the POST result for client-side JS response? – Tetsuya Yamamoto Sep 25 '18 at 02:56
  • 1
    Why are you using `$.getJSON` to call a `POST` action that returns a view that **isn't** JSON? – mjwills Sep 25 '18 at 03:02
  • Does the `alert("se realizó un registro con exito");` trigger in else block? – Masoud Keshavarz Sep 25 '18 at 04:54
  • @MasoudKeshavarz now that you mention it. It doesn't trigger. May be it has something to do with the issue? – Marco Antonio Lea Plaza Soruco Sep 25 '18 at 12:35
  • @mjwills I use the $.getJSON on Post to send the data to another controller, I'm using two controllers and I can't bind them through the asp conventional method. So I'm just using json to send the data to the controller and register the data to the database, on Post it doesn't really need to return anything. – Marco Antonio Lea Plaza Soruco Sep 25 '18 at 12:35
  • @AzarShaikh Sadly it doesn't work, I just tried it but it still behaves as the ActionResult – Marco Antonio Lea Plaza Soruco Sep 25 '18 at 12:36
  • https://api.jquery.com/jquery.getjson/ - `Load JSON-encoded data from the server using a GET HTTP request` You should not use that to call a `POST` endpoint. And you should not use that to return HTML (since it is designed to return JSON). You are basically trying to hammer in a nail with a chainsaw. – mjwills Sep 25 '18 at 12:36
  • @mjwills Haha I'll try that. Also I hope you don't mind me using "You are basically trying to hammer in a nail with a chainsaw." as a phrase. PS: Could you please give me a link to read about Http requests? I'd appreciate it, Thank you – Marco Antonio Lea Plaza Soruco Sep 25 '18 at 14:44
  • https://stackoverflow.com/questions/14678187/jquery-ajax-send-json-and-return-html may be worth reading. – mjwills Sep 25 '18 at 23:29

1 Answers1

0

There are some bad practices in this but to get it to work as intended I did the following.

function GetHabitoId() {
    $.getJSON("/AntecedentesMvc/GetHabitos", function (o) {
        $("#Hid").val(o);
    });
}

The issue was that val() was getting no parameters!