ASP.NET MVC: how to validate an Ajax form with specified UpdateTargetID?
I'm trying to figure out how to show validation errors after a user submits an Ajax form that has its own set of properties UpdateTargetID
.
I don't understand how to update the Ajax form with validation errors without returning the Create
PartialView in the div results
. If the form is valid, then it should return a Records
PartialView.
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
a source to share
You cannot show validation errors unless you update the portion of the DOM containing the elements with validation messages, which in your case is a form. This way you can put the whole form and result div into a partial that will be updated. You can also take a look at this post .
a source to share