Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ModelState.Clear is not working

I have a simple contact form built in MVC that uses the Html helper class to generate the textbox and drop down. I'd like to clear the values of the textboxes and drop down list to as it was when the page was rendered using a get (only after the inquiry was submitted properly).

I am using the method ModelState.Clear() to perform this clean up but my form values still remain, any idea on what I am doing wrong here? Upon success it does display the message in the code. Below you will find a copy of the code from my controller.

Thanks for taking the time!

[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);
}
like image 984
TheWebGuy Avatar asked Feb 16 '12 14:02

TheWebGuy


People also ask

How do I clear ModelState errors?

If you are getting your Model from a form and you want to manipulate the data that came from the client form and write it back to a view, you need to call ModelState. Clear() to clean the ModelState values. The reason is that normally, you want to postback to the client the form with all the errors.

How do I know if my ModelState is valid?

Below the Form, the ModelState. IsValid property is checked and if the Model is valid, then the value if the ViewBag object is displayed using Razor syntax in ASP.Net MVC.

How does ModelState IsValid work?

ModelState. IsValid indicates if it was possible to bind the incoming values from the request to the model correctly and whether any explicitly specified validation rules were broken during the model binding process. In your example, the model that is being bound is of class type Encaissement .

What does ModelState remove do?

Remove(KeyValuePair<String,ModelState>)Removes the first occurrence of the specified object from the model-state dictionary.


1 Answers

In case of success simply redirect to your GET action following the Post-Redirect-Get pattern:

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);
}

or since I am not a big fan of TempData (because it uses Session and personally I always disable it in my applications), you could simply pass a query string parameter to the Contact GET action like (success=1) and inside this action prepare the success message.

like image 200
Darin Dimitrov Avatar answered Sep 20 '22 17:09

Darin Dimitrov