1

I have class:

EmployeeListViewModel with property List<Int32> EmployeeIDs.

I need transfer with get request.

I do not want to see a request like EmployeeIDs[]=1&EmployeeIDs[]=2 ...

I want to specify a tag which has a short name of this parameter

example:

empl[]=1&empl[]=2
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Mediator
  • 14,951
  • 35
  • 113
  • 191
  • There is a `Bind Attribute` on asp.net mvc. Maybe it can help you: http://stackoverflow.com/questions/4316301/asp-net-mvc-2-bind-a-models-property-to-a-different-named-value – Felipe Oriani Jan 07 '13 at 16:00

2 Answers2

1

You could decorate the controller action argument with the [Bind] attribute and specify a prefix:

public ActionResult Index([Bind(Prefix = "empl[]")] int[] employeeIDs)
{
    ...
}

Now the following request will be correctly bound:

empl[]=1&empl[]=2
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • This way I know, and if it is in the class? – Mediator Jan 07 '13 at 16:17
  • That's not supported by the default model binder. If you need to support such scenario you will have to write a custom model binder. But usually I wouldn't bother with that. I would simply match my view model property name with the expected query string name. It's much easier and one of the uses of a view model. – Darin Dimitrov Jan 07 '13 at 16:25
0

Sounds like you're using Model Binding and want to customize the way binding happens. Unfortunately, on a Model class, I don't know of a way to use attributes to accomplish this, but you can accomplish what you want using a custom binder.

You'll need to implement the IModelBinder interface, and then use a [ModelBinder] attribute on your Controller's action method.

More details here: http://dotnetslackers.com/articles/aspnet/Understanding-ASP-NET-MVC-Model-Binding.aspx

sblom
  • 26,911
  • 4
  • 71
  • 95