Marco Dinatsoli
Marco Dinatsoli

Reputation: 10580

not all paths have value while redicting

i am using mvc4

i have a method in my controller. i want this method to redict the use to another method in another controller. i tried this

public ActionResult Building(int buildingID) {
    if((buildingID == 0) || (buildingID<0))
        return HttpNotFound();
    else
        RedirectToRoute(new { controller = "Building", action = "details?id=" + buildingID });
}

but i got this exception

not all paths have value

what am i doing wrong please?

Upvotes: 0

Views: 47

Answers (3)

Harrison
Harrison

Reputation: 3963

Youre else statement doesn't return an ActionResult

This RedirectToRoute(new { controller = "Building", action = "details?id=" + buildingID }); does not return a value. If RedirectToRoute returns an ActionResult you can just add the return keyword in front of it to use it.

Upvotes: 3

Christian Phillips
Christian Phillips

Reputation: 18769

return RedirectToRoute(new { controller = "Building", action = "details?id=" + buildingID });

Also, I think you can use, but I need to double check

return RedirectToAction("details", "Building", new { id = buildingID  });

Also, you could change this line (just to clean up the code)...

if((buildingID == 0) || (buildingID<0))

To..

if(buildingID < 1)

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1038940

You need to return in the else statement:

return RedirectToRoute(new 
{ 
    controller = "Building", 
    action = "details", 
    id = buildingID 
});

Also notice that I am passing the id as part of the anonymous object instead of hardcoding it as some query string parameter to the action name.

Upvotes: 4

Related Questions