Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

364
Views
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 answers
Answer question

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 Report

0

you could just remove the error

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

if (ModelState.IsValid)
{
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!