2012-09-04 35 views
4

比如我http://localhost:1338/category/category1?view=list&min-price=0&max-price=100如何獲得鑑於當前路由的URL參數值在ASP .NET MVC

上在我看來,我要呈現某種形式的

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

我想要什麼是從當前頁面鏈接獲取view參數值,用它來構造表單獲取請求。我試過@using(Html.BeginForm("Action", "Controller", new RouteValueDictionary { { "view", ViewContext.RouteData.Values["view"] } }, FormMethod.Get))但它沒有幫助。

回答

3

你應該還是有從視圖中訪問Request對象:

@using(Html.BeginForm(
    "Action", 
    "Controller", 
    new RouteValueDictionary { 
     { "view", Request.QueryString["view"] } }, FormMethod.Get)) 
+0

非常感謝。這正是我所鎖定的。 – Dmytro

0

從MVC的角度來看,你會想從控制器傳遞值到頁面例如

public ActionResult ViewCategory(int categoryId, string view) 
{ 
    ViewBag.ViewType = view; 
    return View(); 
} 

然後在你看來,你的訪問@ViewBag.ViewType,你需要將其轉換爲一個字符串,雖然,因爲它會被默認objectViewBag是一個動態的對象)。

12

我已經找到了解決辦法in this thread

@(ViewContext.RouteData.Values["view"]) 
+0

這與ROUTE PARAM的工作,這是我所需要的。 '''Request.Params [「paramName」]'''沒有** NOT **與ROUTE PARAM一起工作。 –