如何在ASP .NET MVC中获取当前路由的url参数值

Dmy*_*tro 5 c# asp.net-mvc razor

例如,我在页面上 http://localhost:1338/category/category1?view=list&min-price=0&max-price=100

在我看来,我想渲染一些形式

@using(Html.BeginForm("Action", "Controller", new RouteValueDictionary { { /*this is poblem place*/ } }, FormMethod.Get))
{
    <!--Render some controls-->
    <input type="submit" value="OK" />
}
Run Code Online (Sandbox Code Playgroud)

我想要的是view从当前页面链接获取参数值以使用它来构造表单获取请求.我试过@using(Html.BeginForm("Action", "Controller", new RouteValueDictionary { { "view", ViewContext.RouteData.Values["view"] } }, FormMethod.Get))但它没有帮助.

Dan*_*iel 15

在这个帖子中找到了解决方案

@(ViewContext.RouteData.Values["view"])
Run Code Online (Sandbox Code Playgroud)


Joe*_*ton 5

您仍然应该可以从视图中访问 Request 对象:

@using(Html.BeginForm(
    "Action", 
    "Controller", 
    new RouteValueDictionary { 
        { "view", Request.QueryString["view"] } }, FormMethod.Get))
Run Code Online (Sandbox Code Playgroud)


Ire*_*ren 5

您不能直接在 ASP .NET Core 中访问 Request 对象。这是一种方法。

@ViewContext.HttpContext.Request.Query["view"]
Run Code Online (Sandbox Code Playgroud)