How to clear a Modelstate error with specific property?

Asked

Viewed 79 times

1

I created Asp.net mvc core 2.2 project with individual account.

Follows code:

[BindProperty]
public InputModel Input { get; set; }

public class InputModel
{
    [Required(ErrorMessage = "O campo \"Cartão\" é obrigatório")]
    [Display(Name = "Cartão:")]
    public string Card { get; set; }
    public List<SelectListItem> Cards { get; set; }
}

if (ModelState.ContainsKey("Input.Card"))
    ModelState["Input.Card"].Errors.Clear();

With this code above does not work, the following code works:

ModelState.Clear(); // Limpa todos

I’ve tried that way and it doesn’t work:

foreach (var modelValue in ModelState.Values)
{
    modelValue.Errors.Clear();
}

Still the same mistake in ModelState.

I’m unable to clear a specific property error, some solution ?

1 answer

1

I found the solution:

if (ModelState.ContainsKey("Input.Card"))
    ModelState.Remove("Input.Card");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.