简体   繁体   English

Ajax jQuery并不总是在Controller上执行操作

[英]Ajax jQuery not always executes action on Controller

I have the Controller method below made in ASP.NET MVC 3 (C#) who returns a PartialView: 我在ASP.NET MVC 3(C#)中制作了下面的Controller方法,该方法返回PartialView:

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

The method that I need to execute on submit form is: 我需要在提交表单上执行的方法是:

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

My partialView contains this code: 我的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>

In my JS file I run this code on #btnSubmitMailInsert button: 在我的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);
});
}

My problem is focused on Ajax request. 我的问题集中在Ajax请求上。 Rarely I can make the "happy way", where on submit click, the event is activated on jQuery, calls the method "submitNewEmail()", that calls an Ajax, executes the method on controller and pass with success. 我很少能做出“快乐的方式”,在单击提交时,该事件在jQuery上激活,调用方法“ submitNewEmail()”,该方法调用Ajax,在控制器上执行该方法并成功通过。 But not so... It always returns with fail, not because error returned by controller method, but simply because ajax doesn't runs properly, doesn't execute the method on controller, even with a breakpoint inserted there (on VS2010). 但是,并非如此...它总是以失败返回,不是因为控制器方法返回了错误,而是因为ajax无法正常运行,即使在其中插入了断点(在VS2010上),也不会在控制器上执行该方法。 In this JS code posted by me here is an attempt to alternatively solve this problem, unsuccessful. 在我发布的此JS代码中,尝试替代解决此问题,但未成功。 The original code is: 原始代码是:

    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.");
    }
});

I left the "console.log(msg)" temporary, just to solve this problem. 我暂时保留了“ console.log(msg)”,只是为了解决此问题。

Can someone of you tell me what is happening, or to point where is my error? 你们中的某人可以告诉我发生了什么,还是指出我的错误在哪里?

I found the problem. 我发现了问题。 Everything was right, but some detail damaged the code: window.location.reload(); 一切都很好,但是一些细节破坏了代码:window.location.reload();

I made some changes in my code, that looks like it: 我对代码进行了一些更改,如下所示:

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

The another way is right too, the only relevant change was: 另一种方法也是正确的,唯一相关的更改是:

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

Thanks for trying to help me. 感谢您尝试帮助我。 This helped me so much. 这对我有很大帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM