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?
You need to create your own ModelBinder
.
Have a look at:
Bind a model property to a different named query string field
Asp.Net MVC 2 - Bind a model's property to a different named value
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With