2012-04-20 92 views
2

当我点击提交按钮,我直接去我的行动。以下2不会发生:MVC3 - Razor,Ajax.BeginForm - 客户端 - OnBegin

  1. 我的客户端的beginForm方法此时未被调用。我想要的。
  2. Request.IsAjaxRequest是假的,即使我的模型的形式输入

试图寻找这个问题的答案。相同的代码适用于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); 

我不显眼的JavaScript设置为true在web.config中:

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

回答

2

With MVC3不显眼的Ajax助手:

  • Ajax.ActionLink
  • Ajax.RouteLink
  • 阿贾克斯.BeginForm
  • Ajax.BeginRouteForm

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

所以你需要包括JS文件,而不是MicrosoftAjax那些 (MicrosoftAjax文件被弃用MVC3,而不是与MVC4测试发货):

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

天色不早了类,将检查了这一点作为我回来了。 – 2012-04-20 20:38:18

+0

感谢您的帮助。我现在使用jQuery $ .ajax。以下内容也有帮助: http://stackoverflow.com/questions/5410055/using-ajax-beginform-with-asp-net-mvc-3-razor – 2012-04-23 05:09:00