ASP MVC清除无效modelState后的model.var1值

问题描述 投票:0回答:1

发布表单后如果有任何错误(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);
    }
c# asp.net-mvc post modelstate
1个回答
0
投票

写两个

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

希望这会奏效

© www.soinside.com 2019 - 2024. All rights reserved.