2
當我點擊提交按鈕,我直接去我的行動。以下2不會發生:MVC3 - Razor,Ajax.BeginForm - 客戶端 - OnBegin
- 我的客戶端的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);
我不顯眼的JavaScript設置爲true在web.config中:
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
天色不早了類,將檢查了這一點作爲我回來了。 – 2012-04-20 20:38:18
感謝您的幫助。我現在使用jQuery $ .ajax。以下內容也有幫助: http://stackoverflow.com/questions/5410055/using-ajax-beginform-with-asp-net-mvc-3-razor – 2012-04-23 05:09:00