I have an action method that I would either return JSON from one condition or redirect to another condition. I thought I could do this by returning an ActionResult from my method, but this leads to the error "not all code paths return a value"
Can someone tell me what I am doing wrong? Or how to achieve the desired result?
Here is the code below:
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Login(User user) { var myErrors = new Dictionary<string, string>(); try { if (ModelState.IsValid) { if (userRepository.ValidUser(user)) { RedirectToAction("Index", "Group"); //return Json("Valid"); } else { return Json("Invalid"); } } else { foreach (KeyValuePair<string, ModelState> keyValuePair in ViewData.ModelState) { if (keyValuePair.Value.Errors.Count > 0) { List<string> errors = new List<string>(); myErrors.Add(keyValuePair.Key, keyValuePair.Value.Errors[0].ErrorMessage); } } return Json(myErrors); } } catch (Exception) { return Json("Invalid"); } }
Edit: to clarify, I already tried return RedirectToAction("Index", "Group"); as suggested in the answers, but does nothing. The breakpoint in action that I am redirecting to does not fall.
c # asp.net-mvc jsonresult redirecttoaction
Davedev
source share