Steve N
Steve N

Reputation: 319

Unable to retrieve querystring data

In my bus service MVC project I need to be able to display a bus route stops only when i go to the route table. I have this ActionLink sending the querystring and the page to the route table: @Html.ActionLink("Route Stops", "index", "snRouteStops", new { id=item.busRouteCode}, null)
But when I get to my bus route stops controller I need it to throw me back to the route lists if there isn't one selected. This part works fine but when I click a route to view the stops nothing happens. Here is my code in the actionResult of the busRouteStops controller:

public ActionResult Index()
        {
            string busRouteCode = "";

            if (string.IsNullOrWhiteSpace(Request.QueryString["busRouteCode"]))
            {
                if (Request.Cookies["busRouteCode"] == null)
                {
                    return RedirectToAction("index", "snBusRoutes");
                }
                else
                {
                    busRouteCode = Request.Cookies["busRouteCode"].ToString();                    
                }    
            }
            else
            {
                busRouteCode = Request.QueryString["busRouteCode"].ToString();            

            }
             var routeStops = db.routeStops.Include(r => r.busRoute).Where(r => r.busRouteCode == busRouteCode).Include(r => r.busStop);
        return View(routeStops.ToList());
    }
        }    

Upvotes: 0

Views: 612

Answers (2)

Tommy
Tommy

Reputation: 39807

Your main issue is that you are looking for a query string with the name busRouteCode, but your action link is not setup to provide a query string with that name. Your action link -

@Html.ActionLink("Route Stops", "index", "snRouteStops", new { id=item.busRouteCode}, null)

is configured to send the busRouteCode as a parameter with the name id. That means that the URL will look like this:

/snRouteStops/Index?id=myBusRouteCode

Thus, there is no query string with the name busRouteCode

You can do a few things to clean this up.

  1. Standard routing in MVC is configured to use /Controller/Action/id. You can update your index action to be aware that an Id is coming by updating the action method.

//appears id is a string based on your code

public ActionResult Index(string id)
        {
            string busRouteCode = "";

            if(string.IsNullOrEmpty(id))
            {
                if (Request.Cookies["busRouteCode"] == null)
                {
                    return RedirectToAction("index", "snBusRoutes");
                }
                else
                {
                    busRouteCode = Request.Cookies["busRouteCode"].ToString();                    
                }    
            }
            else
            {
                busRouteCode = id;            

            }
             var routeStops = db.routeStops.Include(r => r.busRoute).Where(r => r.busRouteCode == busRouteCode).Include(r => r.busStop);
        return View(routeStops.ToList());
    }
        } 

  1. Change your action link to send the busRouteCode as a parameter named busRouteCode.

@Html.ActionLink("Route Stops", "index", "snRouteStops", new { busRouteCode=item.busRouteCode}, null)

This will generate a link that looks like:

/snRouteStops/Index?busRouteCode=myBusRouteCodeValue

However you proceed, you should not really ever have to use Request.QueryString[] in your MVC controller actions. By configuring your action method to be looking for those query string values(like I did in example one), you can now get strongly typed parameters in your action methods as opposed to the string values in the QueryString[] dictionary. This is especially helpful when expecting an integer as a query string value for instance. The model binder will take care of ensuring that query string value is actually an integer and not a string.

Upvotes: 1

Konstantin Brodin
Konstantin Brodin

Reputation: 142

This code {id=item.busRouteCode} generates query like that: http://snRouteStops/Index?id=somedata where somedata = item.busRouteCode. Try to look for id in query string or just add id to action params, like that:

public ActionResult Index(string id)

Upvotes: 1

Related Questions