8

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.

4 Answers 4

19

I've found the solution in this thread

@(ViewContext.RouteData.Values["view"])
Sign up to request clarification or add additional context in comments.

1 Comment

This worked with ROUTE PARAM which is what I needed. Request.Params["paramName"] did NOT work with ROUTE PARAM.
7

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

@ViewContext.HttpContext.Request.Query["view"]

Comments

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))

1 Comment

Thanks a lot. That's exactly what I was locking for.
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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.