8

I am looking for a way in my view models to shorten property names in the querystring for a search form. For example, the verbose property name could be query but you see q in the querystring.

Currently, I am doing the following to achieve this.

public string Query { get; set; }
public string q
{
  get
  {
    return Query;
  }
  set
  {
    Query = value;
  }
}

I would think it might be easier if there was a data annotation to help with this.

[Querystring(Name="q")]
public string Query { get; set; }

Is there a better way to do this that I am not thinking of or is it possible to code my own data annotation like that?

  • i think it's possible to create a custom attributes like that (i haven't try it myself though :) – andri Feb 23 '13 at 06:22

1 Answers1

3

You need to create your own ModelBinder.

Have a look at:

  1. Bind a model property to a different named query string field

  2. Asp.Net MVC 2 - Bind a model's property to a different named value

Community
  • 1
  • 1
publicgk
  • 3,170
  • 1
  • 26
  • 45