2

I have an ASP.NET MVC app. My app is using T4MVC. In my controller, I need to redirect from one action to another. When I do this, I want to append a query string value. I can successfully redirect without the query string value, but I've been unsuccessful applying a query string value. My actions look like this:

[HttpPost]
[ValidateAntiForgeryToken]
public virtual ActionResult Action1()
{                        
   return RedirectToAction(MVC.MyController.Action2().AddRouteValue("id", "5"));
}

[Route("action-2")]
public virtual ActionResult Action2(string input)
{
  ViewBag.Input = input;
  return View(viewModel);
}

The Action2 works fine when I visit ./action-2. I can also successfully POST to Action1. But, when the redirect doesn't work. I notice in the address bar the following:

/MyController/id

Why? How do I fix this? I just want to redirect back to Action2 but this time with a query string parameter added. What am I missing?

1
  • Why not just return RedirectToAction("Action2", "MyController", new { id = 5});? Commented Nov 9, 2016 at 14:35

2 Answers 2

6

You need to specify the parameter by the name in the action (in this case "input") in order for it to work, see below:

return redirectToAction(MVC.MyController.Action2().AddRouteValue("input", "5"));

or alternatively:

return RedirectToAction("Action2", "MyController", new { input = "myInput"});
Sign up to request clarification or add additional context in comments.

1 Comment

can you kindly check this thread. stackoverflow.com/questions/75826012/…
-1

I try in following way and it works fine for me.

return RedirectToAction("Index", "CustomBuilder", new { usern = "admin" });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.