Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to clear the form after submit,

I try it like this:

C#
[HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Contact(EmailFormModel model)
        {
            string message2 = "There are a few errors";
            if (ModelState.IsValid)
            { 
                message2 = "Thanks! We'll get back to you soon.";
                var body = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>";
                var message = new MailMessage();
                message.To.Add(new MailAddress("nengelen@online.nl")); //replace with valid value
                message.Subject = "Your email subject";
                message.Body = string.Format(body, model.FromName, model.FromEmail, model.Message);
                message.IsBodyHtml = true;
                using (var smtp = new SmtpClient())
                {
                    await smtp.SendMailAsync(message);
                    
                   
                }
               
            }
            if (Request.IsAjaxRequest())
            {
                return new JsonResult { Data = new { success = true, message = message2 } };
            }

            TempData["Message"] = message2;

            ModelState.Clear();
            return View(model);
        }


Thank you
Posted

1 solution

ModelState.Clear actually clear the validation messages from the model state.
You can create a new model and bind with the view.

model= new EmailFormMode()

return view(model)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900