ModelState.IsValid issue on register form

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
We're getting an ModelState error when someone completes the register form on our site.

I've added this code to catch the error:

var errors = ModelState
    .Where(x => x.Value.Errors.Count > 0)
    .Select(x => new { x.Key, x.Value.Errors })
    .ToArray();


and its showing these errors:
{ Key = "AvailableCountries", Errors = {System.Web.Mvc.ModelErrorCollection} }  <Anonymous Type>
{ Key = "AvailableStates", Errors = {System.Web.Mvc.ModelErrorCollection} }  <Anonymous Type>

Although the Country & County dropdowns are enabled and have been selected, the ModelState check is stating that they are empty.

If I disable both dropdowns, I get the same issue.
12 years ago
By default it works without any errors (just tested several times). Have you done any changes? If yes, please describe them.
12 years ago
Changes to source:

Register.cshtml - Set the newsletter field to true and hidden from view
CustomerController.cs - No changes apart from extra code to view ModelState errors

Stepping through the code on Register page load, the correct fields are being enabled and I can see the Countries (1) and Counties (101) on the model.

Once I fill in the form and then click register, all the fields (i.e City enabled / Company enabled) which were set to True on page load, are now set to False. The AvailableCountries and AvailableCounties are also 0, which is where the ModelState fails on first.

I've attached the screenshot here:
http://www.protechsales.co.uk/capture.jpg

I am getting the same issue when using the Create User in the admin section.
12 years ago
It's hard to say where the issue is without debugging

redmorello wrote:
Once I fill in the form and then click register, all the fields (i.e City enabled / Company enabled) which were set to True on page load, are now set to False. The AvailableCountries and AvailableCounties are also 0, which is where the ModelState fails on first.

They should be set to "False". If you want to persist values between requests, then store them in hidden inputs. This is how MVC works. The AvailableCountries and AvailableCounties should not contain any items after postback (these values are repopulated after each postback if no redirection is done)
12 years ago
Got there in the end.

We'd modified the RegisterValidator.cs and added some extra validators, including checking x.AvailableCountries instead of x.CountryId.

Thanks for your help in pointing in the right direction.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.