Hi
i try here to send email using "SendMailAsync" and it works mail is sent and data is saved to database,
but i can't get JS alert of confirmation and when i look at inspect element i found that:
An asynchronous module or handler completed while an asynchronous operation was still pending.
i don't know what is the problem here and how can i make code works to let me get js alet, i use knocout js to save data to database and sweetalert to get alerts
Controller code:
using SurgiNovi.Models.SurgiNoviDbContext; using System.Net; using System.Net.Mail; using System.Text; using System.Web.Http; using System.Web.Http.Description; namespace SurgiNovi.Controllers { public class MessagesController : ApiController { readonly SurgiNoviCon db = new SurgiNoviCon(); // POST api/Terms [ResponseType(typeof(contactUs))] public IHttpActionResult PostTerm(contactUs message) { if (!ModelState.IsValid) { return BadRequest(ModelState); } db.ContactUS.Add(message); db.SaveChanges(); Sent(); return Ok(message); } private void Sent() { MailMessage message; message = new MailMessage(); message.From = new MailAddress("s@mhafez.net", "S Admin", Encoding.UTF8); message.To.Add(new MailAddress("ahmed.mo.amin@gmail.com", "HR", Encoding.UTF8)); message.Body = "Test"; message.Subject = "Test sub"; SmtpClient client; client = new SmtpClient("mail.mhafez.net"); client.Credentials = new NetworkCredential("s@mhafez.net", "xxxx"); client.Port = 26; client.SendMailAsync(message); } } }
View Code:
@using (Html.BeginForm()) { @Html.AntiForgeryToken()<hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" })<div class="col-md-12" data-bind="with:newmessage"><div class="col-md-6"><div class="b-form-row"><label class="b-form-vertical__label" for="name">Name</label><div class="b-form-vertical__input"> @Html.TextBoxFor(m => m.Name, new { @class = "form-control", style = "font-weight: bold", data_bind = "value:Name" }) @Html.ValidationMessageFor(m => m.Name, "", new { @class = "text-danger" })</div></div><div class="b-form-row"><label class="b-form-vertical__label" for="email">Email</label><div class="b-form-vertical__input"> @Html.TextBoxFor(m => m.Email, new { @class = "form-control", style = "font-weight: bold", data_bind = "value:Email" }) @Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })</div></div><div class="b-form-row"><label class="b-form-vertical__label" for="website">Position</label><div class="b-form-vertical__input"> @Html.TextBoxFor(m => m.Position, new { @class = "form-control", style = "font-weight: bold", data_bind = "value:Position" }) @Html.ValidationMessageFor(m => m.Position, "", new { @class = "text-danger" })</div></div><div class="b-form-row"><label class="b-form-horizontal__label">Country</label><div class="b-form-horizontal__input"> @Html.DropDownListFor(model => model.Country_Id, (SelectList)ViewBag.Countries, " ", new { @class = "form-control", style = "font-weight: bold", data_bind = "value:Country_Id" }) @Html.ValidationMessageFor(model => model.Country_Id, "", new { @class = "text-danger" })</div></div><div class="b-form-row"><label class="b-form-vertical__label" for="title">Institute</label><div class="b-form-vertical__input"> @Html.TextBoxFor(m => m.Institute, new { @class = "form-control", style = "font-weight: bold", data_bind = "value:Institute" }) @Html.ValidationMessageFor(m => m.Institute, "", new { @class = "text-danger" })</div></div></div><div class="col-md-6"><div class="b-form-row"><label class="b-form-vertical__label">Message</label><div class="b-form-vertical__input"> @Html.TextAreaFor(m => m.Message, new { @class = "form-control", style = "font-weight: bold; height: 264px;", rows = 6, data_bind = "value:Message" }) @Html.ValidationMessageFor(m => m.Message, "", new { @class = "text-danger" })</div></div><div class="b-form-row"><input type="button" value="send" class="b-btn f-btn b-btn-md b-btn-default f-primary-b b-btn__w100" data-bind="click:$parent.save" /></div></div></div> } @section scripts{ <script src="~/Scripts/knockout-3.3.0.js"></script><script src="~/Scripts/sweet-alert.min.js"></script><script src="~/Scripts/App/ContactUSViewModel.js"></script><script>$(document).ready(function () { ko.applyBindings(new termViewModel()); });</script> }
JS Code:
var Mesaage = function () { var self = this; self.Name = ko.observable(); self.Email = ko.observable(); self.Position = ko.observable(); self.Country_Id = ko.observable(); self.Institute = ko.observable(); self.Message = ko.observable(); return self; }; function termViewModel() { var self = this; self.newmessage = ko.observable(new Mesaage()); //Get all terms self.Mesaages = ko.observableArray(); //Post new term self.save = function () { $.ajax({ type: "post", url: "/api/Messages", data: ko.toJSON(self.newmessage), success: function (data) { self.Mesaages.push(data);
// code stop here textboxes don't clear text and js alert don't shown ?
self.newmessage(new Mesaage()); swal("Thanks!", "We got youe message!", "success"); }, contentType: "Application/json" }); }; }
hope to find help for my problem please ?