【问题标题】:ASP MVC clear model.var1 value after invalid modelState无效 modelState 后 ASP MVC 清除 model.var1 值
【发布时间】: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


【解决方案1】:

两个都写

ModelState.Remove("Captcha");
model.Captcha = string.Empty;

希望这会奏效

【讨论】:

    猜你喜欢
    • 2018-04-10
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 2016-03-09
    • 1970-01-01
    • 1970-01-01
    • 2017-05-27
    相关资源
    最近更新 更多