2

I've two action method in the following controller-

public class VisitMasterController 
{
    public ActionResult StartBrVisit()
    {
        string id=(Request.QueryString["id"].ToString(); //value=null here
    }

    public ActionResult BrNotPresent()
    {
         return RedirectToAction("StartBrVisit","VisitMaster" , new { id = "id", name = "name" });
    }
{ 

After Redirect, Request.QueryString["id"] returns null.

My default route config is-

         context.MapRoute(
         "BR_default",
         "BR/{controller}/{action}/{id}",
         new { action = "Index", id = UrlParameter.Optional },
         new[] { "OpinionLeader.Areas.BR.Controllers" } //add this line
     );

Any help?

4
  • Are you using the default routes with .../{id}? And why not just use public ActionResult StartBrVisit(int id)? Commented Apr 30, 2016 at 7:40
  • @StephenMuecke - yes, there is a default route with name id. I've updated my question. I need to use string here. Commented Apr 30, 2016 at 7:47
  • Then just make the method public ActionResult StartBrVisit(string id) (and you could add string name as well). You id value is added as a route value, not a query string value so you could also use Request.RequestContext.RouteData.Values["id"] Commented Apr 30, 2016 at 7:52
  • Got the point. Please post it as answer with code snippet (if possible). Thank you. Commented Apr 30, 2016 at 7:55

1 Answer 1

1

You have defined a route with a parameter named id so when you use new { id = "id" }, the RedirectToAction() method finds a match and adds the value as a route value, not a query string value (in the case of name, there is no match, so its value is added as a query string value). You could access it using

string id = (string)Request.RequestContext.RouteData.Values["id"]

However, it would be far easier to add a parameter to your method

public ActionResult StartBrVisit(string id)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.