Pricey
Pricey

Reputation: 5929

MVC model binding in controller action when given a querystring with special character keys

I am using a third party library for a Grid which uses fixed querystring parameters as shown below.

/Home/GetData/?$skip=0&$top=10

These parameters have a $ in their key and I wanted to know if there is a way to still have the MVC model binding work for these parameters.

i.e.

applying them to this action (which won't compile because of the $ in the parameter names.

public ActionResult GetData(int $skip, int $top)
{
...
return View();
}

Upvotes: 0

Views: 538

Answers (1)

Pricey
Pricey

Reputation: 5929

Thanks to Andrei for pointing me in the right direction.

The below solutions both do the trick.

Via prefix alias model binding

public ActionResult GetData([Bind(Prefix = "$top")]int top = 0, [Bind(Prefix = "$skip")]int skip = 0)
{
    return View();
}

By Request object to get the Querystring values

public ActionResult GetData()
{
    var topParam = Request.QueryString["$top"];
    var skipParam = Request.QueryString["$skip"];

    var top = 0;
    int.TryParse(topParam, out top);

    var skip = 0;
    int.TryParse(skipParam, out skip);

    return View();
}

Upvotes: 1

Related Questions