0

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();
}
3
  • 4
    You might need a custom model binder for that. That would be a good solution. Quick and dirty way though is to parse query string inside action Commented May 22, 2014 at 10:45
  • 1
    Oh, and quick search revealed following thing: ActionParameterAlias Nuget package. Never saw it in action frankly, but might be worth a try. Commented May 22, 2014 at 10:49
  • @Andrei thanks that pointed me in the right direction, I have added what I just tried and both seem to work ok Commented May 22, 2014 at 10:58

1 Answer 1

1

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();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Did not know about this BindAttribute, quite useful thing. Cool!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.