Sorry if this has been addressed. I just can’t get it to work. I have a email contact form. I have the form working ok. It is sending an email. However, after the form is submitted I was either a message to pop up or be redirected to a different page saying that their email was sent successfully and we’ll respond as we can. I’ve tried RedirectActionTo and some other things, but I cannot get it to work. Here is my code.
public ActionResult ContactForm(ContactModel emailModel)
{
MailMessage oMail = new MailMessage();
oMail.From = new MailAddress("no-reply@domain.com", "Web Contact Form");
oMail.To.Add("email@domain.com");
oMail.Subject = emailModel.Subject;
string body = "Name: " + emailModel.Name + "\n"
+ "Email: " + emailModel.Email + "\n"
+ "Website: " + emailModel.Website + "\n"
+ "Phone: " + emailModel.Phone + "\n\n"
+ emailModel.Message;
oMail.Body = body;
if (SendMessage(oMail))
{
return RedirectToAction("Message");
}
else
{
return RedirectToAction("Error");
}
return View();
}
private bool SendMessage(MailMessage oMail)
{
SmtpClient client = new SmtpClient("relay-hosting.secureserver.net");
client.Credentials = new NetworkCredential("email@domain.com", "********", "domaion.com");
try
{
client.Send(oMail);
return true;
}
catch (Exception ex)
{
this.exception = ex;
return false;
} }
}
Thank for your help.
The reason is because the action is completed without waiting for result.
here is simplified version of my implementation
after in your controller you can set up your response such as:
The view is redirecting because i guess you have not split Type of the request over the method. and therefore its going straight to redirect.
this is done by
hope this helps