【问题标题】:Ajax jQuery not always executes action on ControllerAjax jQuery 并不总是在 Controller 上执行操作
【发布时间】:2013-10-29 16:05:49
【问题描述】:

我有以下在 ASP.NET MVC 3 (C#) 中创建的控制器方法,它返回一个 PartialView:

public ActionResult InsertEmail(long idPerson)
    {
        PersonEmailViewModel mail = new PersonEmailViewModel();
        mail.email.Person_idPerson = idPerson;
        return PartialView(mail);
    }

我需要在提交表单上执行的方法是:

[HttpPost]
    public ActionResult InsertNewEmail(PersonEmail mail)
    {
        mail.idPersonEmail = mail.Insert(mail);
        return Json(mail);
    }

我的 partialView 包含以下代码:

@model PlatformLib_MySql.BLL.Person.PersonEmailViewModel
<form action="" id="frmNewEmail" method="post">
<div>
E-mail: @(Html.TextBoxFor(m => m.email.email))
@(Html.HiddenFor(m => m.email.Person_idPerson))
<input type="submit" value="Insert" id="btnSubmitMailInsert" />
<input type="button" value="Cancel" id="btnCancelMailInsert" />
</div>
</form>

在我的 JS 文件中,我在 #btnSubmitMailInsert 按钮上运行此代码:

jQuery("#btnSubmitMailInsert").click(function () {
submitNewEmail();
window.location.reload();
});
function submitNewEmail() {
event.preventDefault();
var mail = {
    email: jQuery("#frmNewEmail #email_email").val(),
    Person_idPerson: jQuery("#frmNewEmail #email_Person_idPerson").val()
};

var request = jQuery.ajax({
    url: '/Person/InsertNewEmail',
    data: mail,
    type: 'POST',
    dataType: 'json',
    cache: false
});

request.done(function (msg) {
    console.log(msg);
});

request.fail(function (msg) {
    console.log(msg);
});
}

我的问题集中在 Ajax 请求上。我很少能做出“快乐的方式”,在提交点击时,事件在 jQuery 上被激活,调用方法“submitNewEmail()”,调用 Ajax,在控制器上执行方法并成功传递。但不是这样......它总是返回失败,不是因为控制器方法返回错误,而仅仅是因为 ajax 无法正常运行,即使在控制器上插入了断点(在 VS2010 上)也不会在控制器上执行该方法。 在我这里发布的这个 JS 代码中,试图替代地解决这个问题,但没有成功。 原代码为:

    jQuery.ajax({
    url: '/Person/InsertNewEmail',
    data: mail,
    type: 'POST',
    dataType: 'json',
    cache: false,
    success: function (result) {
        debugger;
        jQuery("#tblEmail").append("<tr><td>Email inserido</td></tr>");
    },
    error: function () {
        debugger;
        alert("Erro ao inserir e-mail.");
    }
});

我暂时留下了“console.log(msg)”,只是为了解决这个问题。

谁能告诉我发生了什么,或者指出我的错误在哪里?

【问题讨论】:

  • event.preventDefault() 将其移入jQuery("#btnSubmitMailInsert").click(function () {
  • 能否尝试在数据中将ajax请求的内容类型设置为“application/json”和JSON.Stringify(),将其转换为json字符串..
  • @Murali 这只是一个细节。
  • @Saranya 是的,但结果是一样的:-(
  • 你能提供PersonEmail类的类定义吗..只是为了检查

标签: c# javascript jquery asp.net ajax


【解决方案1】:

我发现了问题。一切正常,但一些细节损坏了代码:window.location.reload();

我对我的代码做了一些更改,看起来像这样:

    jQuery.ajax({
    url: '/Person/InsertNewEmail',
    data: mail,
    type: 'POST',
    dataType: 'json',
    cache: false
}).done(function (data) {
    // Do something
});

另一种方式也是对的,唯一相关的变化是:

jQuery("#btnSubmitMailInsert").click(function () {
event.preventDefault();
submitNewEmail();
// window.location.reload // -> Commented for testing and everything worked fine.
});

感谢您尝试帮助我。这对我帮助很大。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-05
    • 1970-01-01
    • 2019-12-11
    相关资源
    最近更新 更多