动态生成Ajax.BeginForm [英] Dynamically Generated Ajax.BeginForm

查看:222
本文介绍了动态生成Ajax.BeginForm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET MVC Ajax的形式问题。我生成一个表中的多个阿贾克斯形式,如:

I have a problem with ASP.NET MVC Ajax forms. I generate several Ajax forms in a table, like this:

<% 
foreach (var item in Model)
       {
           var statefulItem = item as StatefulEffectiveItem; %>
    <tr>
        <td>
            <% using (Ajax.BeginForm("ShowAddActivity", "EffectiveItems", new { id = item.Id }, new AjaxOptions() { UpdateTargetId = "details", OnSuccess = "dialogIt" }))
               { %>
            <input type="submit" name="activity" value="Add Activity" />
            <% } %>
        </td>
    </tr>
    <% } %>

到目前为止好。当我发布这些形式之一,它呈现在一个div的局部视图,而这个新的局部视图包含一个新的Ajax.BeginForm(这是不是嵌套在发端形式)。这第二个Ajax.BeginForm绝对是类似于我在表中生成的那些,但是当我将它张贴我没有得到一个阿贾克斯后,而是一个正常的帖子里面把我送到了新的一页。我不明白是什么原因导致这个两Ajax的形式,为什么这个动态生成的Ajax表单的行为像一个普通的HTML表单的区别?

so far so good. When I post one of those forms, it renders a partial view in a div, and this new partial view contains a new Ajax.BeginForm (which is NOT nested in the originator form). This second Ajax.BeginForm is absolutely similar to the ones I generate in the table, but when I post it I don't get an "Ajax post", but a normal post which sends me to a new page. I don't understand what causes the difference between this two Ajax form, why this "dynamically generated" Ajax form behaves like a normal html form?

顺便说一句,我知道我可以在使用Javascript不同的方式做到这一点,但我想知道这是否可以只使用MVC佣工和Ajax库和做的,如果答案是是,在那里我做错了。

By the way, I know I can do this in different ways using Javascript, but I would like to understand if this can be done just using MVC helpers and Ajax Library and, if the answer is "yes", where I do wrong.

非常感谢你,

黄蜂

推荐答案

我不知道它是否可以使用MS AJAX做,但我可以告诉你什么是错的。一旦你更新部分的第一个AJAX,这个更新的部分包含JavaScript来为新的形式注册阿贾克斯。这种JavaScript的不可以执行,因为没有什么是告诉它去执行。你必须调用,对于第一个Ajax请求的成功回调第二种形式注册AJAX JavaScript函数。

I don't know if it can be done with MS AJAX but I can tell you what is wrong. Once you update the partial on the first ajax, this updated partial contains javascript to register ajax for the new form. This javascript is not executed because there's nothing that is telling it to execute. You must call a javascript function that registers ajax for the second form on the success callback of the first ajax request.

这篇关于动态生成Ajax.BeginForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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