0

I am sorry if this has been posted before. I have searched many websites and forms to fix it but I can not get it. I have a simple contact form that allows potential customers to fill out their info click submit and then email a copy of what they inputted to us. I have the email part working fine. However, the part that's not working is message after the form is submitted. I'm try to use a try and catch to display a message when they submit or a err message when it didn't work. Not sure why it is not working. Thank you for the help. My controller code is below.

public ActionResult ContactForm()
{
    return View();
}
public ActionResult Message()
{
    return View();
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ContactForm(ContactModel emailModel)
{
    if (ModelState.IsValid)
    {
    bool isOk = false;
    try
    {
        MailMessage msg = new MailMessage();
        msg.From = new MailAddress("no-reply@bobcravens.com",  "Website Contact Form");
        msg.To.Add("thovden@hovdenoil.com");
        msg.Subject = emailModel.Subject;
        string body = "Name: " + emailModel.Name + "\n"
                    + "Email: " + emailModel.Email + "\n"
                    + "Website: " + emailModel.Website + "\n"
                    + "Phone: " + emailModel.Phone + "\n\n"
                    + emailModel.Message;

        msg.Body = body;
        msg.IsBodyHtml = false;

        SmtpClient smtp = new SmtpClient("smtpout.server.net", 25);
        NetworkCredential Credentials = new NetworkCredential("thovden@hovdenoil.com", "****");
        smtp.Credentials = Credentials;
        smtp.Send(msg);
        msg.Dispose();
        isOk = true
        ContactModel rcpt = new ContactModel();
        rcpt.Title = "Thank You";
                    rcpt.Content = "Your email has been sent.";
                    return View("Message", rcpt);
        }
        catch (Exception ex)
        {
        }
        // If we are here...something kicked us into the exception.
        //
       ContactModel err = new ContactModel();
        err.Title = "Email Error";
        err.Content = "The website is having an issue with sending email at this time. Sorry for the inconvenience. My email address is provided on the about page.";
        return View("Message", err);
        }
        else
        {
            return View();
        }
    }
 }
tereško
  • 58,060
  • 25
  • 98
  • 150
Lars Hovden
  • 317
  • 2
  • 25

2 Answers2

1

The problem is that view that you return:

return View("Messgae", err):

You should return the same view after error on "postback", with the invalid model

return View(err);

One time you call that Message view with MessageModel and in this line you called it with ContactModel, so there must be an error over here...

Side notes:

  • You're catching the Global Exception exception it isn't good practice. Not every exception you can and should handle.
  • You have an isOK flag that doesn't do a thing.
  • Move the exception Handel inside the catch block, not afterwards

Updated based on the comments:

Instead of return View you should Redirect:

return RedirectToAction("Message", err);
return RedirectToAction("Message", rcpt);

public ActionResult Message(ContactModel model)
{
    return View(model);
}
Community
  • 1
  • 1
gdoron
  • 147,333
  • 58
  • 291
  • 367
  • Sorry about the ContactModel and MessageModel. They were straightened out before. I was messing around and then copied and pasted the wrong code. I have updated my code to reflect the correct models – Lars Hovden Jan 31 '12 at 01:47
  • @LarsHovden. What exactly doesn't work? you didn't say anything about the problem. – gdoron Jan 31 '12 at 01:53
  • The part that is not working is the message to be displayed after the form is submitted weather or not the form was emailed successfully or if there was an error – Lars Hovden Jan 31 '12 at 02:02
  • do I put the public ActionResult Message(ContactModel model) { return View(model); } after each RedirectToAction – Lars Hovden Jan 31 '12 at 02:18
  • @LarsHovden. It should be an action-method in the controller level. – gdoron Jan 31 '12 at 02:27
  • Unfortunately it is not redirecting the the Message page nor is it passing along the data – Lars Hovden Jan 31 '12 at 02:54
  • @LarsHovden. I can't debug the code for. just press `F10` until you find it, including in the views. – gdoron Jan 31 '12 at 03:07
0

I would start by emitting the exception so that you can figure out exactly what went wrong. Additionally, you might want to step through the code.

NotMe
  • 87,343
  • 27
  • 171
  • 245