Mesut
Mesut

Reputation: 1875

Clear Only a Field After Postback

I know that we can clear values of all fields by ModelState.Clear(). But what if I'd like to clear only a field, such as incorrect security image code, and all the other fields will remain same.

Any helps will be much appreciated.

Upvotes: 0

Views: 54

Answers (1)

Manoj
Manoj

Reputation: 634

You can do something like this:

if (!ModelState.IsValidField(key))
{
    var emptyValue = new ValueProviderResult(
        string.Empty,
        string.Empty,
        CultureInfo.CurrentCulture);

    ModelState.SetModelValue(
        key,
        emptyValue);
}

Upvotes: 2

Related Questions