ModelState.Clear不起作用

本文关键字:不起作用 Clear ModelState | 更新日期: 2023-09-27 18:24:55

我有一个在MVC中构建的简单联系人表单,它使用Html帮助程序类来生成文本框和下拉框。我想清除文本框和下拉列表的值,就像使用get呈现页面时一样(只有在正确提交查询之后)。

我正在使用方法ModelState.Clear()来执行此清理,但我的表单值仍然保留,你知道我在这里做错了什么吗?成功后,它会在代码中显示消息。下面你会发现我的控制器的代码副本。

谢谢你抽出时间!

[HttpPost]
public ActionResult Contact(ContactUsViewModel model)
{
    if (ModelState.IsValid)
    {
       bool isSuccess = _siteService.CreateInquiry(model.Inquiry);
       if (isSuccess)
       {
           model.SuccessMessage = "Thank you for contacting us.";
           ModelState.Clear();
       }
    }
    model.InquiryTypes = InquiryTypes;
    return View(model);
}

ModelState.Clear不起作用

如果成功,只需按照Post redirect GET模式重定向到GET操作:

public ActionResult Contact()
{
    var model = new ContactUsViewModel
    {
        SuccessMessage = TempData["SuccessMessage"] as string
    };
    return View(model);
}
[HttpPost]
public ActionResult Contact(ContactUsViewModel model)
{
    if (ModelState.IsValid)
    {
       bool isSuccess = _siteService.CreateInquiry(model.Inquiry);
       if (isSuccess)
       {
           TempData["SuccessMessage"] = "Thank you for contacting us.";
           return RedirectToAction("Contact");
       }
    }
    // since you are modifying the value of the InquiryTypes property
    // you need to remove it from the modelstate to avoid getting the 
    // old value rendered by the helpers
    ModelState.Remove("InquiryTypes");
    model.InquiryTypes = InquiryTypes;
    return View(model);
}

或者,由于我不是TempData的铁杆粉丝(因为它使用Session,我个人在应用程序中总是禁用它),您可以简单地将查询字符串参数传递给Contact GET操作,如(success=1),并在该操作中准备成功消息。

除了调用ModelState.Clear()之外,还应该构造一个新的模型对象,例如在中

model = new Vacancies(); 
ModelState.Clear();

这是因为ModelStateController的属性,它不是保存提交表单值的属性。以下是您想要做的:

if (ModelState.IsValid)
{
   bool isSuccess = _siteService.CreateInquiry(model.Inquiry);
   if (isSuccess)
   {
       model = new ContactUsViewModel();  // modified line
       model.SuccessMessage = "Thank you for contacting us.";
   }
}
model.InquiryTypes = InquiryTypes;
return View(model);

Controller.ModelState属性,如MSDN所解释的:

获取模型状态字典对象,该对象包含模型和模型绑定验证的状态。

我在代码示例中所做的只是取出ModelState.Clear()方法调用并放入model = new ContactUsViewMode();。这是一种随意清除ViewModel数据属性的方法。


备选流程

你也可以这样做,并更改你的ViewModel:

public class ContactUsViewModel()
{
    // ... all of your existing view model members
    public void Clear()
    {
        this.SomeStringProperty = string.Empty;
        this.SomeIntProperty = 0;
        // ... so on and so forth
    }
}

然后,您可以只在现有实例上调用Clear(),而不是创建ViewModel类的新实例:

if (ModelState.IsValid)
{
   bool isSuccess = _siteService.CreateInquiry(model.Inquiry);
   if (isSuccess)
   {
       model.Clear();  // modified line
       model.SuccessMessage = "Thank you for contacting us.";
   }
}
model.InquiryTypes = InquiryTypes;
return View(model);

这在游戏中有点晚,但我想出了以下方法ClearModelState:

public void ClearModelState()
{
    var modelStateKeys = ModelState.Keys;
    var formKeys = Request.Form.Keys.Cast<string>();
    var allKeys = modelStateKeys.Concat(formKeys).ToList();
    var culture = CultureInfo.CurrentUICulture;
    foreach (var key in allKeys)
    {
        ModelState[key] = new ModelState { Value = new ValueProviderResult(null, null, culture) };
    }
}