Roge
Roge

Reputation: 3522

How to get Redirect() to work how I want

I've been having some problems getting redirects after login to work how I want. So I came up with the idea to store the current page in the viewbag and use that to redirect, so if the page is mydomain.com/debate/1 I end up with "/debate/1" stored in the viewbad but when I try to redirect its giving me this complaint

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult DebateDetails(Int32)' in 'PoliticalDebate.Controllers.DebateController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

However If I manually type in mydomain.com/Debate/1 it works as expected.

Is there some way to get Redirect to work how I want ?

Upvotes: 0

Views: 80

Answers (2)

Erik Philips
Erik Philips

Reputation: 54638

Since I don't see any code, I can't comment on the way you are trying to do it (which isn't working). On future posts, please post your code. This is one way how you can redirect if you are simply redirecting to the default action on the controller:

return this.RedirectToAction("Index", new { id = debateDetailsID } );

Although it's very hard to tell what you are truly trying to do because you mention debate/1 yet the method being called is DebateDetails which doesn't match (unless you've changed the default routes, again I don't know, there's no code).

Update

According to your comment, you have an error in your MapRoute. Your MapRoute should look like:

routes.MapRoute("Debate Details", 
                "debate/{id}", 
                 new { controller = "Debate", 
                       action = "DebateDetails",
                       // this id value is missing 
                       // so it's not being passed to the controller
                       id = UrlParameter.Optional } );

Upvotes: 2

Zach dev
Zach dev

Reputation: 1620

the answer is there in the complaint, in this particular case you're sending a parameter so, checkout if this is specified

your code must look like redirectToAction("nameOfAction", new {id = yourIdOnViewBag}

Upvotes: 0

Related Questions