【发布时间】:2019-08-01 10:41:03
【问题描述】:
发布表格后如果有任何错误(model.IsValid==false)我想清除1个变量值
但它不起作用
[HttpPost]
[AllowAnonymous]
public async Task<ActionResult> Register(]VMRegister model)
{
if (model.Captcha == string.Empty || model.Captcha.Trim().ToLower() != Session["CAPTCHA"].ToString())
{
ModelState.AddModelError("Captcha", "Invalid captcha entered.");
}
if (ModelState.IsValid)
{
//Insert to DB
return RedirectToAction("Index", "Home");
}
/********************************
If model state is invalid, it populates old values in form
I want to clear captcha value, as new captcha will be appeared
model.Captcha = string.Empty; is not working
*********************************/
model.Captcha = string.Empty;
return View(model);
}
【问题讨论】:
-
ModelState.Remove("Captcha")不起作用,文本框中仍然有旧值 -
简单你可以改变它像那样。模型.Captcha = string.Empty;模型状态.清除();返回视图(模型)
标签: c# asp.net-mvc post modelstate