从控制器返回成功或错误说明以查看



我有一个 ASP.net 引导站点,其中我创建了一个表单控制器,通过该控制器将表单数据发送到电子邮件。问题是我希望在成功发送邮件div打开成功。我不知道该怎么做。请提出任何建议。

首页控制器.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>

你可以做这样的事情:

控制器代码

public ActionResult Index(FormCollection formCollection)
{
    // your previous code
    try
    {
        using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
        {
            smtp.Credentials = new NetworkCredential(emailFrom, password);
            smtp.EnableSsl = enableSSL;
            smtp.Send(mail);
        }
    }
    catch (Exception e)
    {
         ViewBag.HasError = true;
         ViewBag.ErrorMessage = e.Message;
    }
    return View(/*whatever extra info you like*/);
}

查看代码

 @model ViewModel // if you send any view model
 @{
      bool hasError = ViewBag.HasError ?? false;
      string message = ViewBag.ErrorMessage;
 }
 // your question's view code
 @if(hasError)
 {
     <div class="col-md-10 text-danger">
         <p>@message</p>
     </div>
 }

应该这样做,您可以随时改进使用ViewBag

最新更新