2011-09-21 60 views
6

使用MVC3,C#和Razor查看引擎: 我有一個具有Ajax操作鏈接的表單。在選項中,我試圖指定OnBegin和OnComplete javascript函數調用。在這個問題中,我拿出了功能的肉,並簡單地添加警報,以便我可以驗證被擊中的功能。我真正想用這些函數做的是在ajax調用期間使用$ .blockUI。MVC3 - Ajax actionlink - OnBegin,onComplete

相關的代碼如下所示:

@Ajax.ActionLink("my test link", "myAction", new { Controller = "myController" }, new AjaxOptions { OnBegin = "ajaxStart", OnComplete = "ajaxStop" }) 

<script type="text/javascript"> 
    function ajaxStart() { 
     alert("start"); 
    } 

    function ajaxStop() { 
     alert("stop"); 
    } 

</script> 

出於某種原因,按規定這兩個函數永遠不會被調用。我曾嘗試過,沒有括號,例如:

@Ajax.ActionLink("my test link", "myAction", new { Controller = "myController" }, new AjaxOptions { OnBegin = "ajaxStart()", OnComplete = "ajaxStop()" }) 

既不工作。

任何想法?

感謝, 託尼

回答

13

確保你已經包括下面的腳本到你的頁面:

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script> 

,並且已經在你的web.config啓用不顯眼的ajax:

<appSettings> 
    ... 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 
</appSettings> 

在ASP.NET MVC 3中,unobtrusive javascript與jQuery一起使用,所以如果您沒有包含正確的腳本,HTML5 data- *屬性沒有解釋html助手,並且沒有發送AJAX請求。

+0

謝謝。你的回答讓我仔細檢查腳本參考,並發現它是錯誤的。一旦糾正,一切正常。再次感謝! –

3

您可以嘗試在Ajax.ActionLink方法調用之前放置<script>聯盟。 使用此語法的AJAX鏈接:

@Ajax.ActionLink("my test link", "myAction", "myController", new AjaxOptions { OnBegin = "ajaxStart", OnComplete = "ajaxStop" }) 

,記得放jquery.unobtrusive-ajax.min.js進口在您的視圖或_Layout.cshtml

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>