how to access querystring in ASP.Net MVC View?

北城余情 提交于 2019-12-22 01:51:11

问题


How do I access the querystring value in a View?


回答1:


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.




回答2:


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"]



回答3:


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.

e.g:

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

Then, in your View:

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



回答4:


To perform this bad practice in .Net Core:

@Context.Request.Query["SomeParameter"]



回答5:


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



来源:https://stackoverflow.com/questions/2888105/how-to-access-querystring-in-asp-net-mvc-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!