I know the question title is not very clear and specific but I really have no idea how to describe it in a few words. Here comes a little more story about it.
I wanna send an object by POST request to ASP.NET 4 MVC controller, and hopefully it will bind to a C# model. Front end is angular 1.5 and the object is not sent via form submit.
What I want to send
var model = {
id:1,
filters: {
filterBy: ["date"],
warehouseId: [101, 102, 103],
orderStatus: ["completed", "placed"],
currencyId: [140, 209]
}
}
The C# model looks
public class FilterModel{
public int Id { get; set; }
public Dictionary<string, string[]> Filters { get; set; }
}
And the controller function looks like:
[HttpPost]
public ActionResult Export(FilterModel model){
//....
}
Through debugging I can see that when JS object value's array has ony one element, I can successfully get a Dictionary structure with 4 records which are the 4 filters' key and value pairs.
//Seudo code
{Key:"filterBy", Value: "date"}
{Key:"warehouseId", Value: "101"}
{Key:"orderStatus", Value: "completed"}
{Key:"currencyId", Value: "140"}
And I can get the value by:
var warehouseId = model.Filters["warehouseId"][0] // 101
var orderStatus = model.Filters["orderStatus"][0] // completed
However, when the JS object's array has multiple values, the weird part comes. Instead of 4 records, I will get 8 records, which look like:
//Seudo code
{Key:"filterBy", Value: "date"}
{Key:"warehouseId[0]", Value: "101"}
{Key:"warehouseId[1]", Value: "102"}
{Key:"warehouseId[2]", Value: "103"}
{Key:"orderStatus[0]", Value: "completed"}
{Key:"orderStatus[1]", Value: "placed"}
{Key:"currencyId[0]", Value: "140"}
{Key:"currencyId[1]", Value: "209"}
(Please tell me if the description isn't clear enough)
I wonder why this happened and how to fix it? I have changed to use another model for each single filter key value pair, but still curious about the reason of this problem.
Appreciate your wisdom!
Filtersshould be an object containing 4 properties that are collections - e.g.string[] filterBy,int[] warehouseIdetc