7

For example I am on page 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 view parameter 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.

tereško
  • 58,060
  • 25
  • 98
  • 150
Dmytro
  • 16,668
  • 27
  • 80
  • 130

4 Answers4

18

I've found the solution in this thread

@(ViewContext.RouteData.Values["view"])
Community
  • 1
  • 1
Daniel
  • 9,312
  • 3
  • 48
  • 48
  • This worked with ROUTE PARAM which is what I needed. ```Request.Params["paramName"]``` did **NOT** work with ROUTE PARAM. – Ravi Ram Sep 11 '17 at 23:53
6

You can't access Request object directly in ASP .NET Core. Here is a way to do it.

@ViewContext.HttpContext.Request.Query["view"]
Iren Saltalı
  • 516
  • 7
  • 16
5

You should still have access to the Request object from within the view:

@using(Html.BeginForm(
    "Action", 
    "Controller", 
    new RouteValueDictionary { 
        { "view", Request.QueryString["view"] } }, FormMethod.Get))
Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
0

From an MVC perspective, you would want to pass the value from the controller into the page e.g.

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 (ViewBag is a dynamic object).

James
  • 80,725
  • 18
  • 167
  • 237