ASP.NET MVC @Html.Checkbox() issue



即使我在插入操作中没有ajax脚本,我的复选框仍将数据插入数据库后仍会检查。

,但是在我的第一次添加尝试中没有选中它,我想取消选中,当我再次添加其他数据

我的模型:

public bool allowance { get; set; } = false;

我的观点:

@using (Html.BeginForm("PayrollSettingsAccount2/502", "Payroll", new { a = "3", b = "12", c = "44" }, FormMethod.Post, new { id = "form_payrollaccount" }))
{ 
  @Html.CheckBoxFor(model => model.allowance, new { @id = "customCheck1", @class = "custom-control-input"})
}

如果您已选中了复选框allowance并执行表单帖子,则由于模型状态,它将保持检查。我希望状态像在表格发布之前一样,使用 ModelState.Clear();

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Action(MyModel model)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }
    //do stuff
    //removes all the data from the modelstate
    ModelState.Clear();
    return View(model);
}

更多信息:https://patrickdesjardins.com/blog/modelstate-clear-is-is-required-to-display-back-your-model-model-object

最新更新