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

366
Visualizações
How do I change a value in the ModelState, so that it will be valid, using ASP.NET MVC?

I am currently allowing a selectlist to have an initial value of "". The user can choose whether to fill in this option or leave it on the default value.

This selectList works on an ID, where the ID is passed to my controller. However, as an int is not being selected, the ModelState results in False as the input was an empty string rather than an int.

I want to change the value of this empty string to be 0. This would result in the ModelState being True. I have been looking at the ModelState dictionary class, so that I can change the key, value pair before the ModelState is checked. However, I have been unable to use this successfully.
https://msdn.microsoft.com/en-us/library/system.web.mvc.modelstatedictionary(v=vs.118).aspx

Any help would be greatly appreciated.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Try using

ModelState.SetModelValue("PropertyID", new ValueProviderResult("New value", "", CultureInfo.InvariantCulture));

Here is a usefull article.

http://geekswithblogs.net/BobHardister/archive/2013/03/11/retain-and-set-posted-checkbox-value-in-the-mvc-4.aspx

over 4 years ago · Santiago Trujillo Relatório

0

you could just remove the error

if (ModelState.ContainsKey("{key}"))
    ModelState["{key}"].Errors.Clear();

if (ModelState.IsValid)
{
}
over 4 years ago · Santiago Trujillo Relatório
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