2010-04-13 76 views
2

我想弄清楚如何在用戶提交一個具有其UpdateTargetID屬性集的Ajax窗體後顯示驗證錯誤。ASP.NET MVC:如何使用指定的UpdateTargetID驗證Ajax表單?

我很困難如何更新帶有驗證錯誤的Ajax表單而不將Create PartialView返回到results div。如果表單有效,那麼它應該返回部分視圖Records

Create.ascx

<% Using Ajax.BeginForm("Create", 
         "Record", 
         New Record With {.UserID = Model.UserID}, 
         New AjaxOptions With { 
          .UpdateTargetId = "results", 
          .LoadingElementId = "loader" 
         })%>   
    Date Located 
    <%= Html.TextBoxFor(Function(model) model.DateLocated)%> 
    <%= Html.ValidationMessageFor(Function(model) model.DateLocated) %> 

    Description 
    <%= Html.TextBoxFor(Function(model) model.Description)%> 
    <%= Html.ValidationMessageFor(Function(model) model.Description) %> 

    <input id="btnSave" type="submit" value="Create" />   

    <span id="loader" class="loader">Saving...</span> 
<%End Using%> 

Records.ascx

<div id="results"> 
    ... 
</div> 

RecordController.vb

Function Create(ByVal newRecord As Record) As ActionResult 
    ValidateRecord(newRecord) 

    If Not ModelState.IsValid Then 
     Return PartialView("Create", newRecord) 
    End If 

    _repository.Add(newRecord) 
    _repository.Save() 

    Dim user = _repository.GetUser(newRecord.UserID) 
    Return PartialView("Records", user) 
End Function 

回答

3

你不能如果您不刷新包含帶有驗證消息的元素的DOM部分(在您的情況下是表單),則會顯示驗證錯誤。所以你可以把整個表單和結果div放在一個將被刷新的部分中。你也可以看看this post

+0

你可以在這裏介紹一下:http://stackoverflow.com/q/6040534/114029 – 2011-05-18 06:47:02