模型状态.IsValid == false

本文关键字:false IsValid 状态 模型 | 更新日期: 2023-09-27 17:51:08

我正在处理页面上的日志,无论我做什么,我都无法获得我的ModelState。IsValid返回true,这样我就可以让用户登录。在我的控制器中有这段代码:

[AllowAnonymous]
[HttpPost]
public ActionResult ValidateLogon(LogonModel logon)
{
    if (ModelState.IsValid)
    {
        StaffModel staff;
        if (staffAuthenticationService.ValidateLogon(logon.UserName, logon.Password, out staff, out logon))
        {
            staffAuthenticationService.SignIn(staff);
            return Json(new { success = true, redirect = "/Home/ModuleIndex", staff = staff });
        }
        ModelState.AddModelError("", "Username and/or Password may be incorrect");
    }
    return Json(new { errors = GetErrorsFromModelState() });   
}

我的登录模型有一个Staff模型和一个User模型,我的服务方法validateLogon传递了一个登录模型。我试着使用:

var errors = ModelState.SelectMany(x => x.Value.Errors.Select(z => z.Exception));

并记录错误,但是我没有看到错误并且valid总是false。

是否有比使用这个字典对象更好的方法来验证用户,或者我没有找到的工作。我找了好几个小时了。

更新:

public class LogonModel
{
    [Display(Name = "User")]
    [DataType("UserModel")]
    public UserModel User { get; set; }
    [Display(Name = "Staff")]
    [DataType("StaffModel")]
    public StaffModel Staff { get; set; }
    [Required(ErrorMessage = "Please enter your password")]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }
    [Required(ErrorMessage = "Please enter your username")]
    [Display(Name = "User name")]
    public string UserName { get; set; }
    [Display(Name = "Terms Of Use Signed")]
    public bool IsTermsOfUseSigned { get; set; }
    [Display(Name = "Privacy Agreement Signed")]
    public bool IsPrivacyAgreementSigned { get; set; }
    [Display(Name= "System Message")]
    public string SystemLoginMessage { get; set; }
    [Display(Name="Version")]
    public string DatabaseVersion { get; set; }
    [Display(Name="Copyright Date")]
    public string CopyrightDate { get; set; }
    [Display(Name="Web Server")]
    public string WebServer { get; set; }
    [Display(Name = "Last Name")]
    [DataType(DataType.Text)]
    [Required(ErrorMessage = "Required")]
    [StringLength(50, ErrorMessage = "Value cannot exceed 50 characters")]
    public string LastName { get; set; }
    [Display(Name = "Date of Birth")]
    [DataType(DataType.Text)]
    [Required(ErrorMessage = "Required")]
    [StringLength(50, ErrorMessage = "Value cannot exceed 50 characters")]
    public string DateOfBirth { get; set; }
    [Display(Name = "Last 4 digits of XXXX SSN")]
    [DataType(DataType.Text)]
    [Required(ErrorMessage = "Required")]
    [StringLength(50, ErrorMessage = "Value must be 4 numbers")]
    public string SsnSuffix { get; set; }
    public int LogonValidationStep { get; set; }
}

当我将鼠标悬停在if语句上时,我在调试器中看到:IsValid = false然后,当我进入函数时,它正好落在错误信息上。

模型状态.IsValid == false

如果您检查:

if (ModelState.IsValid)

返回false,这很可能意味着视图中使用的模型中的一个必需字段填充了不正确的结果。在您发布模型后,DateOfBirth字段似乎最可疑,因此删除Required属性使我们能够找出导致问题的原因并暂时解决问题。

从现在开始,你有两个选择——保持原样(我不建议这样做),或者进一步调查为什么你没有得到该字段的预期数据。换句话说,不要回避问题,而要努力解决问题并从中吸取教训。

如果我的帖子对你有帮助,你不妨接受它作为一个答案。