Hello StackOverflowers
我遵循了本教程:Ajax Umbraco MVC forms Guide
我的代码在ajax实现之前有效。
经过小的修改后,Ajax会返回成功响应,但控制器断点永远不会命中,因此不会发送任何电子邮件
我在6.1.6,但代码是一样的。有什么想法吗?
这是我的代码:
控制器:
public class QuickContactController : SurfaceController
{
public ActionResult Index()
{
return PartialView("~/Views/Partials/mes-mini-contact.cshtml", new QuickContactModel());
}
[HttpPost]
public ActionResult MESQuickContactPost(Models.QuickContactModel model)
{
string smtp = "internalopenrelay.xxxxx.com";
Document cSettings = new Document(1303);
string from = cSettings.getProperty("fromAddress").Value.ToString();
if (!ModelState.IsValid)
{
//return CurrentUmbracoPage();
return Json(new { sucess = false });
}
string _name = model.Name;
string _email = model.Email;
string _messsage = model.Message;
SmtpClient SMTPClient = new SmtpClient(smtp);
MailAddress FromAddress = new MailAddress(from);
MailAddress ToAddress = new MailAddress("me@emailaddress.com");
MailMessage Message = new MailMessage(FromAddress, ToAddress);
Message.Body = string.Format("New message from: {0}<br>Email Address: {1}<p>{2}</p>", _name, _email, _messsage);
Message.Subject = "New MES Quick Contact Message";
Message.IsBodyHtml = true; ;
try
{
SMTPClient.Send(Message);
}
catch (Exception)
{
throw;
}
SMTPClient.Dispose();
//return RedirectToCurrentUmbracoPage();
return Json(new { success = true });
}
}
型号:
public class QuickContactModel
{
[Required]
public string Name { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[Required]
public string Message { get; set; }
}
部分视图:
@model UmbracoUsStaging1.Models.QuickContactModel
@using (Ajax.BeginForm("MESQuickContactPost", "QuickContactController", new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "submit-status", OnFailure = "ShowError()", OnSuccess = "ShowSuccess()"}))
{
@Html.ValidationSummary(true)
<span class="form-required">@Html.ValidationMessageFor(model => model.Name)</span><br />
@Html.TextBoxFor(model => model.Name, new { @class = "flat-input width174", placeholder = "Name" })<br />
<span class="form-required">@Html.ValidationMessageFor(model => model.Email)</span><br />
@Html.TextBoxFor(model => model.Email, new { @class = "flat-input width174", placeholder = "Email" })<br />
<span class="form-required">@Html.ValidationMessageFor(model => model.Message)</span><br />
@Html.TextAreaFor(model => model.Message, new { @class = "flat-input width174 height85", placeholder = "Message" })<br />
<br />
<a href="javascript:$('form').submit();" class="button-send">Send</a>
<div id="submit-status"></div>
<script>
function ShowError() {
$("#submit-status").html("Oops, there was an Error")
}
function ShowSuccess() {
$("#submit-status").html("Message sent successfully");
$("#Name").val("");
$("#Email").val("");
$("#Message").val("");
}
</script>
}
我的jQuery参考:
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="~/scripts/jquery.validate.min.js"></script>
<script src="~/scripts/jquery.validate.unobtrusive.min.js"></script>
<script src="~/scripts/jquery.unobtrusive-ajax.min.js"></script>