Change Bound Property in OnPost in model is invalid

放肆的年华 提交于 2019-12-06 07:24:50

Once your OnPost method completes and the corresponding View is rendered, the values that are displayed in controls that use the asp-for Tag Helper (or the older HtmlHelper methods) are repopulated from ModelState. This means that even though you are setting a new value for Attempts, it is simply not being used because a value exists in ModelState with the Attempts key.

One way to fix this is to clear the value that's stored in ModelState, using something like this:

public IActionResult OnPost()
{
    if (!IsCorrect())
    {
        ModelState.Remove(nameof(Attempts));
        Attempts++;
        return Page();
    }

    return RedirectToPage("./Index");
}

When a ModelState value doesn't exist, the value is read from the Attempts property on your PageModel implementation, as expected.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!