当我单击提交按钮时,我直接进入我的操作。以下 2 没有发生:
- 此时未调用我的客户端 beginForm 方法。我想要的。
- 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);
我在 web.config 中启用的不显眼的 javascript 设置为 true:
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>