【发布时间】:2016-01-15 18:09:17
【问题描述】:
我有一个 ASP.net 引导站点,我在其中创建了一个表单控制器,通过它我将表单数据发送到电子邮件。问题是我希望在成功发送邮件时打开div 成功。我不知道该怎么做。请有任何建议。
HomeController.cs
[HttpPost]
public ActionResult Index(FormCollection formCollection)
{
var fname = formCollection["fname"];
var lname = formCollection["lname"];
var email = formCollection["email"];
var phone = formCollection["phone"];
var message = formCollection["message"];
string smtpAddress = "smtp.gmail.com";
int portNumber = 587;
bool enableSSL = true;
string emailFrom = email;
string password = "*****";
string emailTo = "******@gmail.com";
string subject = "Hello";
string body = fname + lname + phone + message;
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
if (true)
{
return View();
//return Content("successful");
}
}
}
Index.cshtml
<div class="col-lg-10" id="details_contact" style="display:none;">
<h2>Contact Us</h2>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="well well-sm">
<form class="form-horizontal" method="post">
<fieldset>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i class="fa fa-user bigicon"></i></span>
<div class="col-md-8">
<input id="fname" name="fname" type="text" placeholder="First Name" class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i class="fa fa-user bigicon"></i></span>
<div class="col-md-8">
<input id="lname" name="lname" type="text" placeholder="Last Name" class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i class="fa fa-envelope-o bigicon"></i></span>
<div class="col-md-8">
<input id="email" name="email" type="text" placeholder="Email Address" class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i class="fa fa-phone-square bigicon"></i></span>
<div class="col-md-8">
<input id="phone" name="phone" type="text" placeholder="Phone" class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i class="fa fa-pencil-square-o bigicon"></i></span>
<div class="col-md-8">
<textarea class="form-control" id="message" name="message" placeholder="Enter your massage for us here. We will get back to you within 2 business days." rows="7"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-md-12 text-center">
<button type="submit" class="btn btn-primary btn-lg">Submit</button>
</div>
</div>
</fieldset>
<div id="success" class="alert alert-success">
<strong>Success!</strong> Indicates a successful or positive action.
</div>
</form>
</div>
</div>
</div>
</div>
【问题讨论】:
-
您正在回发一份完整的表格。最简单的方法是重定向到页面(登录页面)并显示成功消息。如果要在页面上说并显示消息,则需要使用 Ajax。
-
@Win 不是他们用视图返回文本的任何方式。
-
您可以在
ViewBag添加您的消息并显示在页面上。 -
@Sushil 可以请做一些代码帮助。
标签: c# jquery asp.net twitter-bootstrap