C# 如何在 ASP .NET MVC 视图中获取当前路由的 url 参数值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12266587/
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
How to get url parameter value of current route in view in ASP .NET MVC
提问by Dmytro
For example I am on page http://localhost:1338/category/category1?view=list&min-price=0&max-price=100
例如我在页面上 http://localhost:1338/category/category1?view=list&min-price=0&max-price=100
And in my view I want to render some form
在我看来,我想呈现某种形式
@using(Html.BeginForm("Action", "Controller", new RouteValueDictionary { { /*this is poblem place*/ } }, FormMethod.Get))
{
<!--Render some controls-->
<input type="submit" value="OK" />
}
What I want is to get viewparameter value from current page link to use it for constructing form get request. I tried @using(Html.BeginForm("Action", "Controller", new RouteValueDictionary { { "view", ViewContext.RouteData.Values["view"] } }, FormMethod.Get))but it doesn't help.
我想要的是view从当前页面链接获取参数值以使用它来构建表单获取请求。我试过了,@using(Html.BeginForm("Action", "Controller", new RouteValueDictionary { { "view", ViewContext.RouteData.Values["view"] } }, FormMethod.Get))但没有帮助。
采纳答案by Joel Etherton
You should still have access to the Request object from within the view:
您应该仍然可以从视图中访问 Request 对象:
@using(Html.BeginForm(
"Action",
"Controller",
new RouteValueDictionary {
{ "view", Request.QueryString["view"] } }, FormMethod.Get))
回答by James
From an MVC perspective, you would want to pass the value from the controller into the page e.g.
从 MVC 的角度来看,您可能希望将值从控制器传递到页面中,例如
public ActionResult ViewCategory(int categoryId, string view)
{
ViewBag.ViewType = view;
return View();
}
Then in your view you an access @ViewBag.ViewType, you will need to cast it to a string though as it will by default be object(ViewBagis a dynamic object).
然后在您的视图中您是一个 access @ViewBag.ViewType,您需要将它转换为一个字符串,尽管默认情况下它是object(ViewBag是一个动态对象)。
回答by Daniel
回答by Iren Saltal?
You can't access Request object directly in ASP .NET Core. Here is a way to do it.
您不能直接在 ASP .NET Core 中访问 Request 对象。这是一种方法。
@ViewContext.HttpContext.Request.Query["view"]

