2

I've run into an odd problem with MVC3 - If I try and use Request.QueryString["bookID"] in my _Layout.cshtml file, it always returns an empty string. If I move this into any of the pages that inherit this page, it works fine.

Most of the problems similar with this seem to be based around webforms and master pages, so the usual fixes don't seem to apply.

Anyone else run into this issue? Should this work and perhaps MVC is just being strange for me?

tereško
  • 58,060
  • 25
  • 98
  • 150
boolean
  • 891
  • 2
  • 14
  • 23

1 Answers1

3

You shouldn't access Request in the view - that's not MVC approach. It it the controller's responsibility.

You don't have to access QueryString directly - that's what routing is for. Add a int bookID parameter to your action method and MVC will pass proper value for you. Than inside the action you can either add it to the ViewBag or, better, populate model class for strongly-typed views.

Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126