Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortening querystring names from view model

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?

like image 555
Gary Turpin Avatar asked Feb 22 '13 19:02

Gary Turpin


1 Answers

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

like image 196
publicgk Avatar answered Oct 21 '22 01:10

publicgk