0

I have a form for sending email in asp.net mvc4 when user clicks on submit button,after the email sent successfully all of text box in my form become empty

my view code:

 @using (Html.BeginForm()) {
 <table cellpadding="5px;">
     <tr>
         <td>@Html.Label("Name")</td>
         <td>@Html.TextBoxFor(m => Model.Name)</td>
         <td class="error">@Html.ValidationMessageFor(m => Model.Name)</td>
     </tr>
     <tr>
         <td>@Html.Label("From")</td>
         <td>@Html.TextBoxFor(m => Model.From)</td>
         <td class="error">@Html.ValidationMessageFor(m => Model.From)</td>
     </tr>
     <tr>
         <td>@Html.Label("Email")</td>
         <td>@Html.DropDownList("Email",new SelectList(Emaillist,"Value","Text"))</td>
         <td class="error">@Html.ValidationMessageFor(m => Model.Email)</td>
     </tr>
     <tr>
         <td>@Html.Label("Subject")</td>
         <td>@Html.TextBoxFor(m => m.Subject)</td>
         <td class="error">@Html.ValidationMessageFor(m => Model.Subject)</td>
     </tr>
     <tr>
         <td>@Html.Label("Text")</td>
         <td>@Html.TextAreaFor(m => m.Body)</td>
         <td class="error">@Html.ValidationMessageFor(m => Model.Body)</td>
     </tr>
      <tr>
         <td> <input id="Submit1" type="submit" value="Send" /></td>
         <td></td>
     </tr>
 </table>
 <br/>
 <br/>

<span style="color: red; font-size: 14px;">@ViewBag.Message</span>   
 }

my controller code:

    [HttpPost]
    public ActionResult Contact(Contact contact)
    {
        if (ModelState.IsValid)
        {

            try
            {
                MailAddress fromAddress = new MailAddress("MyEmail Address");
                MailAddress toAddress = new MailAddress(contact.Email);
                const string fromPassword = "My Password Address";
                string subject = contact.Subject;
                string body = "From: " + ViewBag.Name + "\nEmail: " + contact.From + "\n\n\n Body: " + contact.Body;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "Host";
                smtp.Port = port;
                smtp.EnableSsl = false;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new NetworkCredential(fromAddress.Address, fromPassword);

                MailMessage message = new MailMessage(fromAddress, toAddress);
                message.Subject = subject;
                message.Body = body;

                smtp.Send(message);
                ViewBag.Message = "Your message send successfully ";
                return View("Contact");
            }
            catch (Exception ex)
            {

                ViewBag.Message = "Your message doesn't send, please try again" + "\n" + ex.Message;
            }
        }
        return View();
    }

How can I change value of textbox in controller after send email?

what code should I add after "smtp.Send(message)" in order to my textbox in view become empty?

1
  • did you tried anything in the client side..like jQuery?? Commented Sep 24, 2013 at 11:58

2 Answers 2

1

If you want to display values after sending mail

return View("Contact", contact);

If you want to clear values then Either set values empty manually like contact.Subject = "";...etc and return as above OR you can create new contact object and pass it with view

return View("Contact", new Contact());
Sign up to request clarification or add additional context in comments.

1 Comment

thanks but your code make empty my Contact class.The values that user entered stay in page yet
0

You need to pass your view model back to the view at the end of the ActionResult.

return View("Contact", contact);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.