Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

201
Visualizações
MVC JSON Method not binding Dictionary properly

We are attempting to post data from JavaScript corresponds to a Dictionary<string, List> in C#

public class SubmittedObject {
    public string T;
    public SomeViewModel ViewModel;
}

public class SomeViewModel {
    public int Id;
    public Dictionary<string, List<ItemViewModel>> Items;
    public object Other;
}

public class ItemViewModel { 
    public int id; 
    public string value; 
} //shown as member variables for simplicity

JSON looks like:

{
    "t": "a",
    "viewModel": {
        "id": 0,
        "items": {
            "firstItem": [
                {
                    "id": 1,
                    "value": "1"
                }
            ],
            "secondItem": [
                {
                    "id": 2,
                    "value": "2"
                }
            ]
        },
        "other": null
    }
}

When the JS object gets submitted to

[HttpPost]
public ActionResult Save(string t, SubmittedObject viewModel)
{...}

saveViewModel gets interpreted with the first item in the Dictionary as key="FirstItem[0]" and value of null. SecondValue gets interpreted in like manner with key="SecondItem[0]" and value of null.

The current work-around is to serialize the submission data and submit it as a string; NewtonSoft parses it as expected.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda