2010-09-14 74 views
1

當我得到這個ActionLink的是在Firefox丟失)後的參數列表錯誤使用Ajax.ActionLink MVC2

<%: Ajax.ActionLink(" ", "SelectEditProduct", new { id = item.Id }, new AjaxOptions { UpdateTargetId = "dialog", InsertionMode = InsertionMode.Replace, OnSuccess = "$(\"#dialog\").dialog(\"open\");"}, new { Class="edit"})%> 

這似乎是從的小JavaScript片段我來產生這個錯誤。我雖然逃過了報價,所以我很難過。

回答

2

我在這裏看到的是,您正在使用jQuery和Microsoft AJAX。這兩個沒有理由在同一個項目中混合使用,如果你已經有了jQuery,那麼另一個完全沒用。因此,不是用JavaScript污染您的標記,並想知道如何逃避單引號和雙引號用斜槓和獲得大量的錯誤的,這樣做不顯眼(jQuery的方式):

<%: Html.ActionLink(
    "Some link text", 
    "SelectEditProduct", 
    new { id = item.Id }, 
    new { @class = "edit" } 
) %> 

而且在單獨 js文件:

$(function() { 
    $('a.edit').click(function() { 
     // When a link with class="edit" is clicked 
     // send an AJAX request to the href and replace the result 
     // of a DOM element with id="dialog" with the response 
     // returned by the server 
     // Also when the request completes show a jQuery dialog. 
     $('#dialog').load(this.href, function() { 
      $('#dialog').dialog('open'); 
     }); 
     return false; 
    }); 
}); 
1

你必須通過一個函數的名稱OnSuccess,你可以不寫你的函數就在AjaxOptions。所以,你的代碼更改爲:

<%: Ajax.ActionLink(" ", "SelectEditProduct", new { id = item.Id }, new AjaxOptions { UpdateTargetId = "dialog", InsertionMode = InsertionMode.Replace, OnSuccess = "openDialog"}, new { Class="edit"})%> 

,然後寫相應的JavaScript函數:

openDialog = function() { 
    $("#dialog").dialog("open"); 
} 
相關問題