Search code examples
asp.net-mvcasp.net-mvc-2asp.net-mail

Form is not checking model state or clearing form after submit


I have had a couple of questions on this issue. However, now I have redone my code and almost all of it is working. The only issue is after the for is submitted it is not checking the model state, because even when the form is successful it displays there is an error. Here is my code.

[HttpPost]
    public ActionResult ContactForm(ContactModel emailModel)
    {
        MailMessage oMail = new MailMessage();

        oMail.From = new MailAddress("no-reply@hovdenoil.com", "Web Contact Form");
        oMail.To.Add("email@hovdenoil.com");
        oMail.Subject = emailModel.Subject;
        string body = "Name: " + emailModel.Name + "\n"
                    + "Email: " + emailModel.Email + "\n"                        
                    + "Phone: " + emailModel.Phone + "\n\n"
                    + "Company: " + emailModel.Company + "\n"
                    + "Website: " + emailModel.Website + "\n"
                    + emailModel.Message;
        oMail.Body = body;

        SmtpClient client = new SmtpClient("smtpout.secureserver.net");
        client.Credentials = new NetworkCredential("username", "password");
        client.Send(oMail);

        string message = "There are a few errors";

        if (ModelState.IsValid)
        {
            message = "Thanks! We'll get back to you soon.";
            ModelState.Clear();
        }

        if (Request.IsAjaxRequest())
        {
            return new JsonResult { ContentEncoding = Encoding.UTF8, Data = new { success = true, message = message } };
        }

        TempData["Message"] = message;

        return View();
    }

Solution

  • My bad. I put the If(ModelState.IsValid) too early. Hear is my final code which worked.

    [HttpPost]
    public ActionResult ContactForm(ContactModel emailModel)
    {
        string message = "There are a few errors";
    
        if (ModelState.IsValid)
        {
    
        MailMessage oMail = new MailMessage();
    
        oMail.From = new MailAddress("no-reply@hovdenoil.com", "Web Contact Form");
        oMail.To.Add("email@hovdenoil.com");
        oMail.Subject = emailModel.Subject;
        string body = "Name: " + emailModel.Name + "\n"
                    + "Email: " + emailModel.Email + "\n"                        
                    + "Phone: " + emailModel.Phone + "\n\n"
                    + "Company: " + emailModel.Company + "\n"
                    + "Website: " + emailModel.Website + "\n"
                    + emailModel.Message;
        oMail.Body = body;
    
        SmtpClient client = new SmtpClient("smtpout.secureserver.net");
        client.Credentials = new NetworkCredential("username", "password");
        client.Send(oMail);
    
            message = "Thanks! We'll get back to you soon.";
            ModelState.Clear();
        }
    
        if (Request.IsAjaxRequest())
        {
            return new JsonResult { ContentEncoding = Encoding.UTF8, Data = new { success = true, message = message } };
        }
    
        TempData["Message"] = message;
    
        return View();
    }