0

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!

1
  • If you want to send it in the format in your first snippet, then Filters should be an object containing 4 properties that are collections - e.g. string[] filterBy, int[] warehouseId etc Commented Mar 8, 2018 at 8:36

1 Answer 1

1

I think your model is wrong. Try this:

public class FilterModel
{
    [JsonProperty("id")]
    public long Id { get; set; }

    [JsonProperty("filters")]
    public Filters Filters { get; set; }
}

public class Filters
{
    [JsonProperty("filterBy")]
    public string[] FilterBy { get; set; }

    [JsonProperty("warehouseId")]
    public long[] WarehouseId { get; set; }

    [JsonProperty("orderStatus")]
    public string[] OrderStatus { get; set; }

    [JsonProperty("currencyId")]
    public long[] CurrencyId { get; set; }
}

Hope it help.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the reply. I have figured out using ViewModel like you wrote, but I want to know why that model binding is failed, seeing that C# model is Dictionary<string, string[ ]>, I thought each filter key-value pair can be mapped to a dictionary record because the dictionary value type is string[ ] too. Do you have idea about it?
I cant believe that I stuck because of using the fields instead of properties. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.