0

I have a form that submits to a thank you page, but the problem is that if you hit the back button, the fields are still filled. I want the form to be cleared when user hit submit button, and not sure of the syntax in asp mvc.

In web form I would do

txtName.Text = " ";

In MVC I tried

model.Name = " "; 
model.Name = null;

also tried

ModelState.Clear();

neither works

Here is what I have on the form to clear the field

using (var smtp = new SmtpClient())
{
    var credential = new NetworkCredential
    {
        UserName = "email@email.com",
        Password = "password"
    };
    smtp.Credentials = credential;
    smtp.Host = "smtpout.secureserver.net";
    smtp.Port = 3535;
    smtp.EnableSsl = false;
    await smtp.SendMailAsync(message);
    await smtp.SendMailAsync(CustomerMsg);
    model.Name = null;
    model.Name = "";
    ModelState.Clear();
    return RedirectToAction("Sent");
}
  • MVC is not the issue here; if they hit the back button, the browser is getting the previous page from the cache and isn't hitting your code at all. http://stackoverflow.com/questions/7224524/back-button-not-requesting-asp-net-mvc-get-method – Paul Abbott Apr 05 '16 at 19:29
  • although the question is confusing I am pretty sure OP wants values cleared on submit not on the back button – Maxwelll Apr 05 '16 at 19:31
  • @PaulAbbott is right, changing the model values in the controller after a post is not going to do anything... You need to use AJAX to post the data, then if you get a success back, clear the form and redirect to the "Sent" action – JamieD77 Apr 05 '16 at 20:20
  • I want to delete the text in the form when user hits the submit button which navigates to the thank you page. But if they hit the back button after a successful submission the form is cleared. Or is that just something not to be concerned with. –  Apr 05 '16 at 20:41

2 Answers2

0

just set your values to an empty object - although my background is in javascript I use the following pattern all of the time and assume you could do something really similar here.

function formSubmit(userInfo){
  var name = userInfo.name
  var email = userInfo.email
  userInfo = {}
}
Maxwelll
  • 2,174
  • 1
  • 17
  • 22
0

Pressing the back button will restore the page from cache. You could try adding the following as an attribute to your action:

[OutputCache(NoStore = true, Duration = 1)]

This will force the page to retrieve from the server but depending on what else is occurring on your page it may not be ideal for your needs.