简体   繁体   中英

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.

You can do something like this:

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

    ModelState.SetModelValue(
        key,
        emptyValue);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM