2

I have the following action method:

 public ActionResult SignUp(Player player)
 {
        if (ModelState.IsValid) 
        {...}
 }

The problem is that ModelState.IsValid always returns true even if I have errors in my Player model. In the Player class I have decorated some of the properties with data annotations for string lenth, etc. If I invoke the TryValidateModel(player) method before I invoke the ModelState.IsValid it works fine, it returns false. Any help is appreciated.

The Player model looks like this:

public class Player
{
    public int PlayerID
    {
        get;

        set;
    }

    [Required(ErrorMessage = "Name is required.")]
    [StringLength(10, ErrorMessage = "Name must be under 11 characters.")]
    public String Name
    {
        get;

        set;
    }

    [Required(ErrorMessage = "Password is required.")]
    [StringLength(10, ErrorMessage = "Password must be under 11 characters.")]
    public String Password
    {
        get;

        set;
    }
    ...
}
Zoliqa
  • 1,015
  • 2
  • 15
  • 36
  • 1
    can you post your view, specifically your BeginForm and such. As well please tell us how you are initially rendering your view. Are you using RenderPartial or is there another action method that does this for you? All come into play with model binding. – Code Jammr Dec 15 '11 at 13:16
  • The error will most certainly be in your view, it will be the naming of an input field or something and it can't bind back to your model. Please show us your SignUp view. – eth0 Dec 22 '11 at 10:18
  • is that you whole method signature? Is it decorated with an [HttpPost] attribute so that it knows to post? – Fran Sep 13 '12 at 20:26

1 Answers1

0

Please take a look at the following article, they successfully answered the same question you have: ModelState.IsValid always returning true

If no direct solution (I hope there is), then some ideas will certainly be useful.

Please let me know if this is of help.

Display Name
  • 4,672
  • 1
  • 33
  • 43