简体   繁体   中英

how to access querystring in ASP.Net MVC View?

如何访问视图中的querystring值?

It is not a good design to access query parameters in a view. The view should use the model provided by the controller. So the controller reads query parameters and passes them to the view. If you want to ignore this rule you could always do this in your view:

<%= Request["SomeParameter"] %>

But I would strongly discourage you from doing so.

In View, you can access it directly. No need to write any code in Controller, though you can.

For example - If your querystring has parameter named id, something like ?id=1

Razor syntax:

@Request.QueryString["id"]

I would read the querystring value in your Controller, and then set that value to a property in your ViewBag. The ViewBag property can then be read in from your view.

eg:

ViewBag.MyQSVal = Request.QueryString["myValue"];

Then, in your View:

@if(ViewBag.MyQSVal == "something"){ ... }

.Net 核心中

@Context.Request.Query["SomeParameter"]

As Darin suggested you should not use Querystring in view. But one thing is you can access Request variable in your view because its Asp.Net and if you access it you have all the functions and member that are present there

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM