Mediator
Mediator

Reputation: 15378

How binding big array to small name?

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

Upvotes: 1

Views: 91

Answers (2)

sblom
sblom

Reputation: 27343

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

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038750

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

Upvotes: 1

Related Questions