我的代码调用错误函数,即使它按照应该的那样命中Action url也是如此。这是Jquery ajax代码:
function ContactDialog(action, controller) {
var url = '/' + action + '/' + controller;
$("#ContactDialog").dialog({
autoOpen: true,
hide: "fade",
show: "bounce",
height: $(window).height() / 2,
width: $(window).width() / 2,
title: "Send an email",
buttons: [{
text: "Send", click: function () {
$.ajax({
url: url,
data: $("form").serialize(),
context: this,
contentType: 'application/html; charset=utf-8',
dataType: "json", //xml, json, script and html
success: function () {
$(this).dialog("close");
sendConfirmMessage('msgSent');
},
error: function () {
sendConfirmMessage('msgNotSent');
}
});
},
}, {
text: "Cancel", click: function () {
$(this).dialog("close");
}
}]
});
这是html / razor代码:
<div style="margin-top: 10px; visibility: hidden;" id="contactLink">
<h3><a href="#" onclick="ContactDialog('SendEmail', 'SendMail')">Contact me</a></h3>
</div>
<div id="ContactDialog" style="display: none;">
@using (Ajax.BeginForm(null, null, new AjaxOptions { UpdateTargetId = "UpdaterDiv" }, new { id = "contactForm" }))
{
@Html.LabelFor(model => model.subject)
@Html.TextBoxFor(model => model.subject, new { @class = "AboutControls" })
<br />
@Html.ValidationMessageFor(model => model.subject)
<br />
@Html.LabelFor(model => model.from)
@Html.TextBoxFor(model => model.from, new { @class = "AboutControls" })
<br />
@Html.ValidationMessageFor(model => model.from)
<br />
@Html.LabelFor(model => model.body)
@Html.TextAreaFor(model => model.body, new { @class = "AboutControls AboutControlTxtArea" })
<br />
@Html.ValidationMessageFor(model => model.body)
<hr />
<br /><br />
}
</div>
<div style="display: none;" title="Info" id="msgSent">
<p>Thank you, your message has been sent </p>
</div>
<div style="display: none;" title="Info" id="msgNotSent">
<p>There was an error sending you message, please try again </p>
</div>
这是作为成功命中的Action方法,它确实调用此方法,但它返回错误,就好像它不调用thisAction方法一样。知道什么可能是错的吗?
public PartialViewResult SendMail(EmailModel model)
{
string host = ConfigurationManager.AppSettings.Get("SMTPHOST");
int port = Convert.ToInt16(ConfigurationManager.AppSettings.Get("SMTPPORT"));
string receiver = ConfigurationManager.AppSettings.Get("TOADDRESS");
string password = ConfigurationManager.AppSettings.Get("PASSWORD");
SmtpClient smtp = new SmtpClient(host, port);
smtp.Credentials = new NetworkCredential(receiver, password);
smtp.EnableSsl = true;
MailMessage mailMsg = null;
try
{
if (ModelState.IsValid)
{
mailMsg = new MailMessage(model.from, receiver, model.subject, model.body);
smtp.Send(mailMsg);
}
}
catch (Exception ex)
{
SendErrorMail("m@mail.net", "...", "Exception", ex.ToString());
}
return PartialView("_About");
}
答案 0 :(得分:0)
这可能会有所帮助......如果我错了,人们会纠正我的!似乎成功和错误函数缺少一些参数。理想情况下,我们至少应将data
作为参数传递。
$.ajax({
url: url,
data: $("form").serialize(),
context: this,
contentType: 'application/html; charset=utf-8',
dataType: "json", //xml, json, script and html
success: function (data) {
console.log("Here is the the data - ", data);
$(this).dialog("close");
sendConfirmMessage('msgSent');
},
error: function (data) {
console.log("Oops, didn't work - ", data);
sendConfirmMessage('msgNotSent');
}
});
我还会注销一些信息以查看进度。希望这有帮助!
答案 1 :(得分:0)
您的ajax选项指定dataType: "json",
,但您调用的方法返回部分视图(html)。将选项更改为
dataType: "html",
附注:使用UrlHelper
构建您的网址
var url = '@Ul.Action(action, controller)';