0

I have a client ( in this case POSTMAN) that is posting collection of object. The properties of the object are not known in advance so I cannot use concrete C# model.

So I am using Dictionary<string,object> that represent a single object where Key will be the property name and Value will be the value of the property. Since client is posting collection i am using List<Dictionary<string,object>>

ISSUE

In controller's action method each dictionary has Key however corresponding value is NULL

POSTMAN

enter image description here

Fiddler shows

model%5B0%5D.FirstName=foo&model%5B0%5D.LastName=bar&model%5B1%5D.FirstName=james&model%5B1%5D.LastName=smith

Quick watch in model:

enter image description here

I tried using JObject, ExpandoObject as model with no luck

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
LP13
  • 30,567
  • 53
  • 217
  • 400
  • Why not just `POST` JSON instead? For example: `[{"key1":"value1","key2":"value2"}]` – DavidG Mar 12 '20 at 15:53
  • i dont have control over client – LP13 Mar 12 '20 at 15:55
  • Have you tried `model[0][0].Key = "FirstName"`, `model[0][0].Value = "Foo"`? – ColinM Mar 12 '20 at 15:56
  • Does this answer your question? [ASP.NET MVC Binding to a dictionary](https://stackoverflow.com/questions/5191303/asp-net-mvc-binding-to-a-dictionary) – ColinM Mar 12 '20 at 15:59
  • That is how its being posted..check the Fiddler capture i posted – LP13 Mar 12 '20 at 15:59
  • The point I'm making is that `FirstName` and `LastName` aren't valid properties on the `Dictionary` object. You need to assign the `Key` and `Value` properties. `FirstName` may simply be getting captured by the model binder which then possibly makes an assumption that `FirstName` should be the `Key` value. I'm working on a code sample at the moment to assist further. – ColinM Mar 12 '20 at 16:06

1 Answers1

1

I changed the model type from List<Dictionary<string, object>> to List<Dictionary<string, string>> and it worked

    [HttpPost]
    public IActionResult Update([FromForm]List<Dictionary<string, string>> model)
    {
        return Ok();
    }
LP13
  • 30,567
  • 53
  • 217
  • 400