Reputation: 4789
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
Reputation: 1038820
Sure:
public static MvcHtmlString Foo(this HtmlHelper htmlHelper)
{
var value = htmlHelper.ViewContext.HttpContext.Request["paramName"];
...
}
Upvotes: 8
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
Reputation: 2405
You can access the querystring via the HttpContext object. Like so...
string itemVal = System.Web.HttpContext.Current.Request.QueryString["item"];
Upvotes: 5