MVC3 - Razor,Ajax.BeginForm - 客户端 - OnBegin

时间:2012-04-20 20:09:40

标签: asp.net-mvc-3 razor ajax.beginform

当我点击提交按钮时,我直接转到我的操作。以下2次没有发生:

  1. 此时未调用我的客户端beginForm方法。我想要的。
  2. 即使我的模型具有表单输入
  3. ,Request.IsAjaxRequest也为false

    试图找到这个问题的答案。相同的代码在MVC2中有效,所以我必须在这里遗漏一些东西。

    引用了2个MS js文件:

    <!-- MS AJAX -->
    <script type="text/javascript" src="/Scripts/MicrosoftAjax.js"></script>
    <script type="text/javascript" src="/Scripts/MicrosoftMvcAjax.js"></script>
    

    表格代码:

    @{
            using (Ajax.BeginForm(ActionNames.Index, ControllerNames.CustomerSearch, new {Area = ""}, new AjaxOptions
                                                                                                          {
                                                                                                              HttpMethod = "Post",
                                                                                                              OnBegin = "CustomerSearch.beginForm",
                                                                                                              OnSuccess = "CustomerSearch.successForm"
                                                                                                          }, new {id = "CustomerSearchForm"}))
            {
    
    ... form items ...
    }
    }
    

    页面上显示的表格:

    <form action="/CustomerSearch" data-ajax="true" data-ajax-begin="CustomerSearch.beginForm" data-ajax-method="Post" data-ajax-success="CustomerSearch.successForm" id="CustomerSearchForm" method="post">
    
    ... Other Form Items ...
    <input type="image" src="/App_Themes/Main/Images/ResponseAction/Buttons/btn_submit.gif" alt="Submit" id="SubmitButton" />
    </form>
    

    这是我的CustomerSearch.js,页面加载警报(2)显示正常:

    var CustomerSearch = {
        enums: {
            buttonId: "SubmitButton",
            searchResultsContainerId: "CustomerSearchResults"
        },
        beginForm: function () {
            alert(1);
            var $button = $("#" + CustomerSearch.enums.buttonId);
            jMessage("Processing request...", $button, true, false);
            return true;
        },
        successForm: function (context) {
            var $button = $("#" + CustomerSearch.enums.buttonId);
            var $searchResults = $("#" + CustomerSearch.enums.searchResultsContainerId);
            var data = context.get_data();
    
            jMessageHide();
            $searchResults.html(data).fadeIn(500);
        }
    };
    
    alert(2);
    

    我在web.config中启用了不显眼的javascript:

    <add key="ClientValidationEnabled" value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
    

1 个答案:

答案 0 :(得分:2)

With MVC3不引人注目的Ajax助手:

  • Ajax.ActionLink
  • Ajax.RouteLink
  • Ajax.BeginForm
  • Ajax.BeginRouteForm

正在使用jquery.unobtrusive.ajax.js文件。

所以你需要包含那个js文件而不是MicrosoftAjax文件 (MicrosoftAjax文件在MVC3中已弃用,未随MVC4 beta一起提供):

<script type="text/javascript" src="/Scripts/jquery.unobtrusive-ajax.min.js"></script>