asp.net-mvc c# razor url 参数视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6514292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c# razor url parameter from view
提问by Shaokan
Why does Request["parameterName"]
returns null within the view? I know I can get it from the controller but I have to make a little check in the View. I am using ASP.NET MVC 3.
为什么Request["parameterName"]
在视图中返回 null?我知道我可以从控制器获取它,但我必须在视图中进行一些检查。我正在使用 ASP.NET MVC 3。
回答by Wouter Simons
You can use the following:
您可以使用以下内容:
Request.Params["paramName"]
回答by Daniel
I've found the solution in this thread
我在这个线程中找到了解决方案
@(ViewContext.RouteData.Values["parameterName"])
回答by Ravi Ram
@(ViewContext.RouteData.Values["parameterName"])
worked with ROUTE PARAM.
与 ROUTE PARAM 合作。
Request.Params["paramName"]
did not work with ROUTE PARAM.
不适用于 ROUTE PARAM。
回答by Jamie Dixon
If you're doing the check inside the View, put the value in the ViewBag
.
如果您在 View 中进行检查,请将值放入ViewBag
.
In your controller:
在您的控制器中:
ViewBag["parameterName"] = Request["parameterName"];
It's worth noting that the Request
and Response
properties are exposed by the Controller
class. They have the same semantics as HttpRequest
and HttpResponse
.
值得注意的是,Request
和Response
属性由Controller
类公开。它们与HttpRequest
和具有相同的语义HttpResponse
。