我有以下操作方法:
public ActionResult SignUp(Player player)
{
if (ModelState.IsValid)
{...}
}
问题是即使我的 Player 模型中有错误,ModelState.IsValid 也总是返回 true。在 Player 类中,我用字符串长度等的数据注释修饰了一些属性。如果我在调用 ModelState.IsValid 之前调用 TryValidateModel(player) 方法,它工作正常,它返回 false。任何帮助表示赞赏。
Player 模型如下所示:
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;
}
...
}