ModelState.Clear不起作用

时间:2012-02-16 14:01:41

标签: c# asp.net-mvc asp.net-mvc-3

我有一个在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);
}

4 个答案:

答案 0 :(得分:5)

如果成功,只需按照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)在这个动作中准备成功消息。

答案 1 :(得分:0)

这是因为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 property,正如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);

答案 2 :(得分:0)

这在游戏中有点晚,但我想出了以下方法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) };
    }
}

答案 3 :(得分:0)

您应该在调用ModelState.Clear()旁边构建一个新的模型对象,例如在

model = new Vacancies(); 

ModelState.Clear();