【问题标题】:How can I change value of textbox in controller in ASP.NET MVC 4如何在 ASP.NET MVC 4 中更改控制器中文本框的值
【发布时间】:2013-09-26 18:07:42
【问题描述】:

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

我的查看代码:

 @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>   
 }

我的控制器代码:

    [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();
    }

发送邮件后如何更改控制器中文本框的值?

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

【问题讨论】:

  • 你有没有在客户端尝试过任何东西……比如 jQuery??

标签: c# asp.net asp.net-mvc email asp.net-mvc-4


【解决方案1】:

您需要在 ActionResult 结束时将视图模型传回视图。

return View("Contact", contact);

【讨论】:

    【解决方案2】:

    如果你想在发送邮件后显示值

    return View("Contact", contact);
    

    如果您想清除值,则可以手动将值设置为空,如 contact.Subject = "";...etc 并按上述方式返回,或者您可以创建新的联系人对象并将其与视图一起传递

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

    【讨论】:

    • 谢谢,但您的代码使我的联系人类为空。用户输入的值仍保留在页面中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-25
    • 1970-01-01
    • 2011-05-25
    • 2018-02-15
    • 1970-01-01
    相关资源
    最近更新 更多