如果ModelState无效,则ASP.NET MVC AJAX更改UpdateTargetId [英] ASP.NET MVC AJAX change UpdateTargetId if ModelState is invalid

查看:69
本文介绍了如果ModelState无效,则ASP.NET MVC AJAX更改UpdateTargetId的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个内部有两个局部视图的视图.

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>

第一个局部视图"RenderMatchesListRowUserControl"呈现一个简单的div元素(用于匹配列表),第二个局部视图"AddNewMatchUserControl"呈现一个表单以在列表下方创建新的匹配项:

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:

AddNewMatchUserControl的来源:

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&uuml;gen:</label>
        <%= Html.TextBox("DurationBetweenMovesInDays")%>
        <%= Html.ValidationMessage("DurationBetweenMovesInDays", "*")%>
    </p>
    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
<% } %>

取决于ModelState,控制器返回新匹配项的局部视图或表单的局部视图,以显示模型错误.

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");
        }

它可以正常工作,直到ModelState变为无效为止.在这种情况下,表单将在匹配列表的末尾呈现,因为updatetargetid引用包含匹配列表的div元素.为了避免这种情况,必须将updatetargetid更改为引用包含表单的div元素.但是我不知道该怎么做.

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.

推荐答案

好的.无法以这种方式实现这一目标.我用jQuery解决了.

OKay. There is no way to achieve this in that way. I solved it with jQuery.

这篇关于如果ModelState无效,则ASP.NET MVC AJAX更改UpdateTargetId的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆