Jimmyt1988
Jimmyt1988

Reputation: 21136

Using Request.QueryString inside C# View Helper

I want to write a View Helper that will know about the parameters in the URL, but I cannot get access to the Request.QueryString:

    public static MvcHtmlString SortDirectionArrow(this HtmlHelper html, string column)
    {
        string desc = Request.QueryString["desc"].ToString();
        string currentSortedColumn = Request.QueryString["sort"].ToString();

        if (desc == "False" && currentSortedColumn == column)
        {
            return new MvcHtmlString("desc");
        }
        else{
            return new MvcHtmlString("");
        }
    }

And you can't just create a new version of HTTPRequestBase because it's an interface:

        HttpRequest Request = new HttpRequest(); // or            
        HttpRequestBase Request = new HttpRequestBase();

http://msdn.microsoft.com/en-us/library/system.web.httprequest.querystring(v=vs.110).aspx

Upvotes: 2

Views: 7650

Answers (1)

Nitin Varpe
Nitin Varpe

Reputation: 10694

Try this

var desc = html.ViewContext.HttpContext.Request.QueryString.Get("desc");

Found this on can request querystring be accessed from htmlhelper

Upvotes: 4

Related Questions