User475983607 posted
jagjit saini
Hi
How the below 2 lines works in the below code
- return Json(new { success = true });
- return Json(phone, JsonRequestBehavior.AllowGet);
[HttpPost]
public JsonResult Create(Phone phone)
{
if (ModelState.IsValid)
{
db.Phones.Add(phone);
db.SaveChanges();
return Json(new { success = true });
}
return Json(phone, JsonRequestBehavior.AllowGet);
}
Thanks
Pretty simple. The unknown "Phone" type in JSON format is return when the model is invalid. Otherwise, phone is added to the DbContext, saved and { success = true } is returned.
There is nothing stopping you from running this code your the Visual studio debugger. The AllowGet is unnecessary since the Action is defined as a POST. Plus the logic should check if phone exists before adding a new record.