How to get url parameter value of current route in view in ASP.NET MVC

For example, I am on the page http://localhost:1338/category/category1?view=list&min-price=0&max-price=100

And, in my opinion, I want to display some form

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

I want to get the value of the view parameter from the current page link in order to use it to build a request for a form. I tried @using(Html.BeginForm("Action", "Controller", new RouteValueDictionary { { "view", ViewContext.RouteData.Values["view"] } }, FormMethod.Get)) , but this does not help.

+4
source share
3 answers

You must have access to the Request object from the view:

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

I found a solution in this thread

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

From an MVC point of view, you would like to pass a value from the controller to the page, for example.

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

Then in your view you will get @ViewBag.ViewType access, you will need to pass it to the string, although by default it will be object ( ViewBag is a dynamic object).

0
source

Source: https://habr.com/ru/post/1432310/


All Articles