2011-04-25 63 views
0

我試圖在被動態加載從一個jquery Ajax調用生成視圖一個jQuery模式對話框來驗證表單驗證MVC與JQuery.Ajax

有頁面上已有的一種形式,這形式是在其他形式下創建,並且所有內容似乎都是開放的,但我不確定是否可以在第二種形式上使用常規MVC驗證,因爲它是動態創建的。

這裏是主要的

<% Html.EnableClientValidation(); %> 
    <% using (Html.BeginForm("UpdateFund", "AdministerFundFeature", FormMethod.Post, new { enctype = "multipart/form-data" })) { %> 

    <fieldset> 

     ... 
    <input type="submit" value="Submit" /> 
    </fieldset> 
<%} %> 

    <div id="GrantRecipDialog" title="Add/Edit Grant Recipiant"> 
     <div id="GrantRecipContent"></div> 
    </div> 

上的按鈕,點擊此功能的HTML視圖被激發新形式

var url = "<%: Url.Action("AddOrUpdateGrantRecip", "AdministerFundFeature") %>" + "?aGrantId=" + aGrantId + "&aFundId=" + aFundId; 
      $.ajax({ 
       url: url, 
       success: function (data) { 
        $('#GrantRecipContent').html(data); /*place the data here, and rerender the section*/ 
        $('#GrantRecipDialog').dialog("open"); 
       }, 
       error: function() { alert("There was a problem with your request, please resubmit your request."); },//?? 
       complete: function() {} 
      }); 

     } 

這裏populationg股利是獲取在渲染視圖彈出

<% Html.EnableClientValidation(); %> 
<% using (Ajax.BeginForm("updateGrant", "AdministerFundFeature", 
     new AjaxOptions { OnComplete="function(){onGrantRecipUpdate()}", OnFailure="function(){return onGrantFail()}"}, 
     new { @id = "frmID" })) 
    { %> 
<fieldset> 
    ... 

</fieldset> 
<%}%> 

是否有可能掛鉤的MVC驗證彈出,它彈出在第一種形式下工作正常,但我不知道如何明確地告訴MVC提交生成的新的驗證信息

回答

0

我剛纔提出了這個問題,並發現我只需提供我的驗證錯誤轉換爲JSON結果並使用Ajax表單的完整方法處理它們。

我找不到我鏈接到的文章,但確實找到了這個;

http://www.hightech.ir/SeeSharp/aspnet-validation-with-ajax-and-json

+0

另一個要知道,如果你的形式分享共同的字段名(即兩個型號有地址,顯示兩種形式與模型綁定的領域),這會混淆一點。所以,你需要附加一個前綴到其中一個模型來區分它。 – Hammerstein 2011-04-26 00:49:09

1

使用此之後創建形式:

$.validator.unobtrusive.parse($('form'));