Recipes/CreateRecipe was successful but return Json is not returning anything and goes to the next function which is the alert Error CreateRecipe null
script
$scope.createRecipe = function (recipe) {
$http({
method: 'POST',
url: '/Recipes/CreateRecipe',
data: JSON.stringify(recipe)
}).then(function (response) {
if (response!=null) {
$scope.recipeID = response.data;
alert($scope.recipeID)
}
else {
alert("get response failed! " + response.data)
}
}, function (response) { alert('Error CreateRecipe '+response.data); });
};
controller
[HttpPost]
public JsonResult CreateRecipe([Bind(Include = "Id,Name")] Recipe vm)
{
Recipe recipe = new Recipe();
recipe.Name = vm.Name;
db.Recipes.Add(recipe);
db.SaveChanges();
return Json(recipe.Id, JsonRequestBehavior.AllowGet);
}
I tried a lot of things like adding ModelState.IsValid and checking if recipe.Id is null but it wasn't. I also tried changing JsonResult to ActionResult. Sometimes the alert says get response failed! sometimes it's Error CreateRecipe null.
Even if I put recipe.Id in an int before returning it, it still doesn't work.
nullJson(recipe.Id, ..), does recipe.Id have a value?asynctoo, when you open thedblayer, the thread probably doesn't wait forrecipe.Idand returns a null/undefined value back to angularjs.