Shaokan
Shaokan

Reputation: 7684

c# razor url parameter from view

Why does Request["parameterName"] returns null within the view? I know I can get it from the controller but I have to make a little check in the View. I am using ASP.NET MVC 3.

Upvotes: 78

Views: 153778

Answers (6)

Tayfun Yuksel
Tayfun Yuksel

Reputation: 162

.Net Core 7 @Requst.Query["ParamName"]

Upvotes: 2

OrElse
OrElse

Reputation: 9959

@(HttpUtility.UrlDecode(Request.Query["parameterName"].FirstOrDefault()) ?? "")

Upvotes: 0

Ravi Ram
Ravi Ram

Reputation: 24488

@(ViewContext.RouteData.Values["parameterName"])

worked with ROUTE PARAM.

Request.Params["paramName"]

did not work with ROUTE PARAM.

Upvotes: 10

Wouter Simons
Wouter Simons

Reputation: 2906

You can use the following:

Request.Params["paramName"]

See also: When do Request.Params and Request.Form differ?

Upvotes: 184

Daniel
Daniel

Reputation: 9521

I've found the solution in this thread

@(ViewContext.RouteData.Values["parameterName"])

Upvotes: 43

Jamie Dixon
Jamie Dixon

Reputation: 53989

If you're doing the check inside the View, put the value in the ViewBag.

In your controller:

ViewBag["parameterName"] = Request["parameterName"];

It's worth noting that the Request and Response properties are exposed by the Controller class. They have the same semantics as HttpRequest and HttpResponse.

Upvotes: 6

Related Questions