ModelState.IsValid does not exclude required property

后端 未结 7 1280
猫巷女王i
猫巷女王i 2020-12-06 12:22

Im trying to exclude a required property(Password) so the modelstate dont validate that property, but for some reason it still validate even when i try to exclude it.

<
相关标签:
7条回答
  • 2020-12-06 13:03

    You could use an extension method like so:

    public static bool IsValidExclude(this ModelStateDictionary modelState, params string[] exclude)
    {
        foreach (var key in exclude)
        {
            if (modelState.ContainsKey(key))
                modelState.Remove(key);
        }
    
        return modelState.All(m => m.Value.Errors.Count == 0);
    }
    

    Then just call:

    var result = ModelState.IsValidExclude("Password");
    
    0 讨论(0)
提交回复
热议问题