I use a view with two partial views inside.
<div id="matches">
<% foreach (var item in Model)
{ %>
<% Html.RenderPartial("RenderMatchesListRowUserControl", item); %>
<% } %>
</div>
<div id="addMatchFormBox">
<% Html.RenderPartial("AddNewMatchUserControl");%>
</div>
The first partial view "RenderMatchesListRowUserControl" renders a simple div element (for a list of matches), the second one "AddNewMatchUserControl" renders a form to create a new match beneath the list:
Source of AddNewMatchUserControl:
<%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %>
<% using (Ajax.BeginForm("Create", new AjaxOptions
{
UpdateTargetId = "matches",
InsertionMode = InsertionMode.InsertAfter,
OnSuccess = "flashit",
OnFailure = "renderForm"
}))
{%>
<fieldset>
<legend>New Match</legend>
<p>
<label for="DurationBetweenMovesInDays">
Dauer (in Tagen) zwischen den Zügen:</label>
<%= Html.TextBox("DurationBetweenMovesInDays")%>
<%= Html.ValidationMessage("DurationBetweenMovesInDays", "*")%>
</p>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
Depending on the ModelState, the controller returns the partial view for the new match entry or the partial view for the form, to display the model errors.
if (Request.IsAjaxRequest()) {
return ModelState.IsValid ? PartialView("RenderMatchesListRowUserControl", match) : PartialView("AddNewMatchUserControl");
}
It works fine, till the ModelState become invalid. In this case the form will be rendered at the end of the matches list because of the updatetargetid refers to the div element which contains the matches list. To avoid this, the updatetargetid must be changed to refer the div element containing the form. But i have no idea how to do this.