TrustyCoder
TrustyCoder

Reputation: 4789

can request querystring be accessed from htmlhelper

Hi Can query string be accessed in HTMLHelper extension methods. We need to render differently depending on the querystring in the request.

Upvotes: 23

Views: 8122

Answers (3)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038820

Sure:

public static MvcHtmlString Foo(this HtmlHelper htmlHelper)
{
    var value = htmlHelper.ViewContext.HttpContext.Request["paramName"];
    ...
}

Upvotes: 8

Brian Mains
Brian Mains

Reputation: 50728

Yes, through the current context, which is a property on HTML Helper.

public static string DoThis(this HtmlHelper helper)
{
   string qs = helper.ViewContext.HttpContext.Request.QueryString.Get("val");
   //do something on it
}

Upvotes: 36

Bob Black
Bob Black

Reputation: 2405

You can access the querystring via the HttpContext object. Like so...

string itemVal = System.Web.HttpContext.Current.Request.QueryString["item"];

Upvotes: 5

Related Questions