0

I have the following:

@Html.ActionLink("Customer Number", "Search", new { Search = ViewBag.Search, q = ViewBag.q, sortOrder = ViewBag.CustomerNoSortParm, })

With both viewbag properties coming from the URL:

http://localhost:51488/Home/Search?Search=Postcode&q=test&sortOrder=CustomerNo

However the URL produced is:

http://localhost:51488/Home/Search?sortOrder=CustomerNo

with it not picking up either of the ViewBag values.

stats101
  • 1,837
  • 9
  • 32
  • 50

2 Answers2

1

ViewBag doesn't come from the URL. It comes from the controller action. If you want to fetch query string parameters or parameters that were part of a POST request you could use the Request:

@Html.ActionLink(
    "Customer Number", 
    "Search", 
    new { 
        Search = Request["Search"], 
        q = Request["q"], 
        sortOrder = Request["CustomerNoSortParm"] 
    }
)
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

ViewBag let you share data from the controller to the View.

If you want to use data from the URL to create your link, you will need to use the FormCollection of your controller's action (if you build the link inside the controller) or to use directly the HttpContext (which is accessible from the View)

System.Web.HttpContext.Current.Request.QueryString["Search"]
Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341