2011-07-12 21 views
0

我正在使用MVC 3的Ajax表單這是我的看法:攔截阿賈克斯partialview在MVC 3形式

<div id="form_container"> 
    @{ Html.RenderPartial("_FormPartial"); } 
</div> 

這是形式的局部視圖:

using (Ajax.BeginForm("Register", "Account", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "form_container", LoadingElementId = "loading", OnBegin = "animate" })) 
{...code...} 

我的「當出現錯誤時,在我的「帳戶」控制器上註冊「操作將返回相同的部分視圖。但表單提交時正確我用另一個局部視圖替換形式partialview像這樣:

<h1>Success!<h1> 

我希望<h1>由jQuery的攔截,並用fadeIn()呈現。 document.ready()既不工作也不工作,因爲它的局部視圖不再存在,所以在ajax表單上的OnCompleted/OnSuccess屬性也不起作用。

達到此目的的最佳方法是什麼?

回答

1

爲什麼你不能使用的onSuccess回調,像這樣:

@using (Ajax.BeginForm(
    "Register", 
    "Account", 
    new AjaxOptions { 
     HttpMethod = "POST", 
     LoadingElementId = "loading", 
     OnBegin = "animate", 
     OnSuccess = "success" 
    } 
)) 
{ 
    ... 
} 

和您的成功回調:

function success(result) { 
    // Because I have removed the UpdateTargetId property from the AjaxOptions 
    // we have to manually inject the resulting partial HTML into the DOM 

    var success = $('<div/>').html(result).find('h1').length > 1; 
    if (success) { 
     // TODO: do some fancy effect such as fadeIn 
     // I really suck at this effects part, so there's 
     // probably a better way to do this 
     $('#form_container').html(result).hide().fadeIn(2000); 
    } else { 
     $('#form_container').html(result); 
    } 
} 
+0

原來我其實可以使用的onSuccess。你提供的成功函數並沒有被使用,因爲它只是:function success(){$(「h1」)。hide()。fadeIn(「slow」);} – vascop