如何在ASP.NET MVC 4中更改控制器中文本框的值

时间:2022-12-02 09:59:33

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

我有一个用于在用户点击提交按钮时在asp.net mvc4中发送电子邮件的表单,在电子邮件发送成功后,我的表单中的所有文本框都变为空

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?

在“smtp.Send(message)”之后我应该添加什么代码才能使我的文本框变为空?

2 个解决方案

#1


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

如果要清除值,则可以手动将值设置为空,如contact.Subject =“”; ...等,并按上述方式返回或者您可以创建新的联系对象并将其传递给视图

return View("Contact", new Contact());

#2


0  

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

您需要将视图模型传递回ActionResult末尾的视图。

return View("Contact", contact);

#1


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

如果要清除值,则可以手动将值设置为空,如contact.Subject =“”; ...等,并按上述方式返回或者您可以创建新的联系对象并将其传递给视图

return View("Contact", new Contact());

#2


0  

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

您需要将视图模型传递回ActionResult末尾的视图。

return View("Contact", contact);