ASP MVC 在 modelState 无效后清除 model.var1 值
ASP MVC clear model.var1 value after invalid modelState
发布表单后如果有任何错误(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");
model.Captcha = string.Empty;
希望这会奏效
发布表单后如果有任何错误(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");
model.Captcha = string.Empty;
希望这会奏效