热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Ajax.Beginform只提交一次mvc-Ajax.Beginformissubmittingonlyoncemvc

HelloiamcreatinganadvancesearchformwhereanAjax.Beginformiscalledfromjquerywiththe.s

Hello i am creating an advance search form where an Ajax.Beginform is called from jquery with the .submit() event, the Ajax.Beginform returns a partialView and update a div on the page. Now when i press the search button the first time everything works fine and the page is updated properly but when i try to press it again the the form is never submitted and controller action is never called. When i checked firebug i found this error showing in the console every time i press the search button:

您好我正在创建一个高级搜索表单,其中使用.submit()事件从jquery调用Ajax.Beginform,Ajax.Beginform返回partialView并更新页面上的div。现在,当我第一次按下搜索按钮一切正常并且页面正确更新但是当我再次按下它时,表单永远不会被提交,并且从不调用控制器动作。当我检查firebug时,我发现每次按下搜索按钮时都会在控制台中显示此错误:

    TypeError: $.validator.methods[method] is undefined


    result = $.validator.methods[ method ].call( this, val, element, rule.parameters...

And this is my code:

这是我的代码:

The ajax beginfirm from the View:

ajax从视图开始确认:

     @using (Ajax.BeginForm("AdvancedSearch", null, new AjaxOptions { UpdateTargetId = "searchResults", InsertiOnMode= InsertionMode.Replace }, new { id = "advancedSearchForm"}))

The jquery function:

jquery函数:

function submitAdvancedSearch() {    
    if ($("#Name").val().trim() == "" && $("#LastName").val().trim() == "" && $("#Company").val().trim() == "" ) {
            $("#resultError").text("Enter at least one search criteria");
    }
    else {
         $("#resultError").text("");
         $("#advancedSearchForm").submit();
         $("#advancedClientsSearch").prop("hidden", false);
    }
}

And this is the Button:

这是按钮:

    

This is my bundle packages:

这是我的捆绑包:

                bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js",
                    "~/Scripts/jquery-ui-{version}.js",
                    "~/Scripts/jquery.unobtrusive-ajax.js",
                    "~/Scripts/DataTables/jquery.dataTables.js",
                    "~/Scripts/DataTables/jquery.dataTables.min.js"));

                bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.validate.js",
                    "~/Scripts/jquery.validate.unobtrusive.js",
                    "~/Scripts/jquery.validate*"));

NB: i am using MVC5 and jQuery Javascript Library v2.1.3

注意:我正在使用MVC5和jQuery Javascript Library v2.1.3

2 个解决方案

#1


If you not need auto validation then remove "~/bundles/jqueryval" bundle.

如果您不需要自动验证,请删除“〜/ bundles / jqueryval”包。

#2


ok i found out what it was i had set some validation rules that are affecting the functionality once i removed them everything works fine now

好吧,我发现它是什么,我已经设置了一些影响功能的验证规则,一旦我删除它们现在一切正常

        $("#ClientName").rules("add", {
    required: true,
    messages: {
        required: "Client Name can not be empty."
    }
});

$("#ClientLastName").rules("add", {
    required: true,
    messages: {
        required: "Client Last Name can not be empty."
    }
});

$("#ClientCompanyName").rules("add", {
    required: true,
    messages: {
        required: "Company Name can not be empty."
    }
});

i use them for another page but MVC uses default naming so i had a kind of conflict.

我将它们用于另一个页面,但MVC使用默认命名,所以我有一种冲突。


推荐阅读
author-avatar
粪青12_601
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有