![](/img/trans.png)
[英]How can I pass an input/Textbox Field to Controller? ASP.NET MVC and Entity Framework
[英]How can I change value of textbox in controller in ASP.NET MVC 4
當用戶單擊提交按鈕時,我有一個在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)”之后添加什么代碼,以便視圖中的文本框為空?
如果要在發送郵件后顯示值
return View("Contact", contact);
如果您想清除值,則可以手動將值設置為空,如contact.Subject =“”; ... etc並按上述方式返回,或者可以創建新的聯系人對象並將其與視圖一起傳遞
return View("Contact", new Contact());
您需要在ActionResult的末尾將視圖模型傳遞回視圖。
return View("Contact", contact);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.