简体   繁体   English

这个jQuery表单帖子有什么问题?

[英]What's wrong with this jQuery form post?

I have a form which I am trying to submit with Ajax / without reloading the page. 我有一个要在不重新加载页面的情况下使用Ajax提交的表单。

The Ajax call is hitting the server, but it doesn't seem to be calling the function. Ajax调用正在命中服务器,但似乎并未调用该函数。 I've tried both $.post and $.ajax . 我已经尝试了$.post$.ajax Neither seem to work. 似乎都不起作用。 I had something extremely similar working a few weeks back, but now I can't replicate it. 几周前,我有一些极为相似的工作,但现在我无法复制。 (The end goal is to return the model as a partial view, in order to see the validation.) (最终目标是将模型作为局部视图返回,以便查看验证。)

Using Firefox, the controller method is immediately hit. 使用Firefox,将立即触发控制器方法。 Using Chrome, the browser tab seems to lock up. 使用Chrome,浏览器标签似乎已锁定。 It sometimes hits the controller after a delay. 有时会在延迟后撞到控制器。 (The model information is coming over accurately.) (型号信息正在准确传递。)

What am I doing wrong here? 我在这里做错了什么?

I have my partial view here: 我在这里有部分看法:

<% using (Ajax.BeginForm("CreateSimpleReport", "Main", new AjaxOptions { HttpMethod="POST" }, new { id="CreateSimpleReport" })){ %>
   <%: Html.ValidationSummary(false, "Report creation was unsuccessful. Please correct the errors and try again.", new { @class = "validation_summary" })%>
   <% Html.EnableClientValidation(); %>
   <div class="col">
<div class="row">
    <div class="label">
        <%: Html.LabelFor(m => m.Name)%>
    </div>
    <div class="field">
        <%: Html.TextBoxFor(m => m.Name, new { @class = "input texbox" })%>
        <%: Html.ValidationMessageFor(m => m.Name, "*")%>
    </div>
</div>
<div class="row">
    <div class="label">
        <%: Html.LabelFor(m => m.Description)%>
    </div>
    <div class="field">
        <%: Html.TextBoxFor(m => m.Description, new { @class = "input texbox" })%>
        <%: Html.ValidationMessageFor(m => m.Description, "*")%>
    </div>
</div>
<div class="row">
    <div class="label">
        <%: Html.LabelFor(m => m.Quantity)%>
    </div>
    <div class="field">
        <%: Html.TextBoxFor(m => m.Quantity, new { @class = "input texbox" })%>
        <%: Html.ValidationMessageFor(m => m.Quantity, "*")%>
    </div>
</div>
<div class="right"><input type="submit" class="button" value="Create Report" /></div>
</div>
<% } %>

Controller: 控制器:

[HttpPost]
public string CreateSimpleReport(SimpleReportModel model)
{
    if (ModelState.IsValid)
    {
        // do something
        return "success";
    }
    else
    {
        return "failure";
    }
}

And finally, the jQuery: 最后,jQuery:

        $('#CreateSimpleReport').on('submit', function (e) {
            var $this = $(this);
            $.post((this), (this), function(data) {
                alert('finish');
            });
//            $.ajax({
//                type: 'POST',
//                url: (this),
//                data: (this),
//                success: function () {
//                    alert('success');
//                },
//                error: function (jqXHR, textStatus, errorThrown) {
//                    alert('error');
//                }
//            }); // end ajax call
        });

You should not be mixing Ajax.* helpers with jQuery.ajax . 您不应将Ajax.*帮助器与jQuery.ajax混合使用。 If you use the Ajax.BeginForm helper then simply include the jquery.unobtrusive-ajax.js script (ASP.NET MVC 3) or the MicrosoftAjax.js and MicrosoftMvcAjax.js scripts (ASP.NET MVC 2) and subscribe to a success callback in the AjaxOptions . 如果使用Ajax.BeginForm帮助器,则只需包含jquery.unobtrusive-ajax.js脚本(ASP.NET MVC 3)或MicrosoftAjax.jsMicrosoftMvcAjax.js脚本(ASP.NET MVC 2)并订阅成功回调在AjaxOptions

If you decide to use jQuery.ajax manually then use a standard Html.BeginForm: 如果您决定手动使用jQuery.ajax,请使用标准的Html.BeginForm:

<% using (Html.BeginForm("CreateSimpleReport", "Main", FormMethod.Post, new { id = "CreateSimpleReport" })) { %>

and then: 接着:

$(function() {
    $('#CreateSimpleReport').on('submit', function() {
        $.ajax({
            url: this.action,
            type: this.method,
            data: $(this).serialize(),
            success: function(result) {
                alert('AJAX success');
            }
        });

        // make sure you return false to cancel the default event
        return false;
    });
});
$.post((this), (this)

This (no pun intended) makes no sense. (没有双关语)是没有道理的。 You cannot POST to a DOM element with the arguments being a DOM element, too. 您也不能将参数作为DOM元素发布到DOM元素。 Besides that you need to e.preventDefault(); 除此之外,您还需要e.preventDefault(); (or return false; ) to avoid a regular form submission. (或return false; )以避免常规表单提交。 So when your callback would fire, the page has already been reloaded. 因此,当您的回调将触发时,页面已被重新加载。

The easiest way to submit a form via AJAX is by using the jQuery form plugin . 通过AJAX提交表单的最简单方法是使用jQuery表单插件

You are already using Ajax.BeginForm , why do you need jQuery script to do ajax? 您已经在使用Ajax.BeginForm ,为什么还要用jQuery脚本来做ajax?

Have you included jquery.unobtrusive-ajax.js ? 您是否包含jquery.unobtrusive-ajax.js

关于将Microsoft Ajax和验证与自定义jquery ajax混合在一起,我在这里有一个类似的线程: 将Microsoft MVC ajax和验证助手与本机jquery ajax混合在一起

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

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