haagel
haagel

Reputation: 2726

Redirecting to action with a parameter

Let's say I have a website that displays info about books. The info about a specific book can be seen on a page with the url "/Book/Show/1234", where 1234 is the id of the book.

To edit the information about the book, there's a link on the book page that goes to "/Book/Edit/1234".

When done editing, I wan't to redirect the user back to the book page, i.e. back from "/Book/Edit/1234" to "/Book/Show/1234".

My problem is the going back part. I'm trying to use the RedirectToAction() method. I can specify which controller and which action in that controller to direct to (the "/Book/Show" part), but I don't know how to specify the id of the book to go to, i.e. the last part of the url.

The Edit action method recieves the id as a parameter, so I have access to it. I have tried this without success:

return RedirectToAction("Show", "BookController", id);

This redirects to the url "/Book/Show/". As you can see there is no id.

What is a good way to accomplish what I need?

Upvotes: 0

Views: 1518

Answers (1)

Sergey Gavruk
Sergey Gavruk

Reputation: 3558

Try this:

return RedirectToAction("Show", "BookController", new { id = bookId});

where bookId is ID of your book. And Show action looks like:

public ActionResult Show (string id) { ... }

Upvotes: 3

Related Questions