How to either return JSON or RedirectToAction?
Asked Answered
C

4

6

I have an Action Method that I'd either like to return JSON from on one condition or redirect on another condition. I thought that I could do this by returning ActionResult from my method but doing this causes the error "not all code paths return a value"

Can anyone tell me what I'm doing wrong? Or how to achieve the desired result?

Here's 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've already tried to return RedirectToAction("Index", "Group"); as suggested in the answers but it doesn't do anything. The breakpoint in the action I'm redirecting to doesn't get hit.

Chichi answered 14/5, 2010 at 9:30 Comment(0)
N
3

You need to change

RedirectToAction("Index", "Group"); 

to

return RedirectToAction("Index", "Group"); 
Novah answered 14/5, 2010 at 9:34 Comment(2)
Hi Daniel, I've tried this but it doesn't seem to do anything. I put a breakpoint into the Action I'm redirecting to but that doesn't get hit. I've updated the question with this. ThanksChichi
Is this in an action method that only accepts a Post and you are trying to redirect to an action that only accepts a Get? I've been tripped up by that before. Kindness, DanNovah
W
0

you shold return RedirectResult. Change this string

if (userRepository.ValidUser(user))
                    {
                        RedirectToAction("Index", "Group");
                        //return Json("Valid");
                    }

to

if (userRepository.ValidUser(user))
                    {
                        return RedirectToAction("Index", "Group");
                        //return Json("Valid");
                    }

and all will work fine.

Wordsmith answered 14/5, 2010 at 9:36 Comment(0)
M
0

Your missing a return statement:

return RedirectToAction("Index", "Group");

The Controller.RedirectToAction method returns a RedirectToRouteResult and Controller.Json method returns a JsonResult. Both extend ActionResult.

Marketplace answered 14/5, 2010 at 9:37 Comment(0)
N
0

I think the reason your redirect is not getting where you want is that it is directing to an action that accepts only Gets and you are redirecting a Post.

Novah answered 14/5, 2010 at 11:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.