ASP.NET MVC Ajax.BeginForm吃提交按钮PARAMS点击。貌似错误 [英] ASP.NET MVC Ajax.BeginForm eats params of submit button clicked. Looks like bug

查看:104
本文介绍了ASP.NET MVC Ajax.BeginForm吃提交按钮PARAMS点击。貌似错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您使用的是Ajax.BeginForm()与多个提交类似这样的按钮:

If you are using Ajax.BeginForm() with multiple submit buttons similar to this:

// View.aspx
<% using (Ajax.BeginForm("Action", "Controller", 
           new AjaxOptions { UpdateTargetId = "MyControl",  }))
{ %>
    <span id="MyControl">
       <% Html.RenderPartial("MyControl"); %>
    </span>
<% } %>

//MyControl.ascx
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<input name="prev" type="submit" value="prev" />
<input name="next" type="submit" value="next" />
//...

一切都被提交到控制器的罚款,但被点击的PARAMS提交按钮均来自请求缺席。 。换句话说请求[下一步],并请求[preV]总是空。

Everything is submitted to the controller fine but the params for the submit button that was clicked are absent from the Request. In otherwords Request["next"] and Request["prev"] are always null.

我看着给JavaScript的Microsoft.MvcAjax.js,它看起来像函数Sys_Mvc_MvcHelpers $ _serializeForm完全跳过那些类型的输入提交。

I looked in to the JavaScript in Microsoft.MvcAjax.js and it looks like the function Sys_Mvc_MvcHelpers$_serializeForm completely skips over the inputs that are of type 'submit'.

这似乎不符合逻辑的。你还能如何找出哪些按钮被点击?

This doesn't seem logical at all. How else can you find out what button has been clicked?

它看起来像我的错误。有没有逻辑上的理由跳过这些表单参数?

It looks like a bug to me. Is there any logical reason to skip these form parameters?

推荐答案

更新:2009-11-21 我下载了MVC 2版preVIEW 2看去,看看这个问题得到了解决。 我做了一个快速测试,发现了类似的结果,以MVC 2版preVIEW 1。 我不相信它是固定的呢。

UPDATE: 2009-11-21 I downloaded MVC Release 2 Preview 2 and looked to see if this problem was fixed. I did a quick test and found similar results to MVC Release 2 Preview 1. I don't believe it is fixed yet.

更新:2009-08-07 我下载了MVC 2版preVIEW 1看去,看看这个问题得到了解决。 我看到一个新的功能,在脚本MicrosoftMvcAjax.debug.js名为 _serializeSubmitButton ,然后我看到,当Ajax.BeginForm()呈现有一个onclick事件,但是当这个事件触发它产生一个错误输出微软JScript运行时错误:Sys.Mvc.AsyncForm'为空或不是对象

UPDATE: 2009-08-07 I downloaded MVC Release 2 Preview 1 and looked to see if this problem was fixed. I see a new function in the script MicrosoftMvcAjax.debug.js called _serializeSubmitButton and I see that when Ajax.BeginForm() renders the output there is a onclick event but when this event fires it generates an error "Microsoft JScript runtime error: 'Sys.Mvc.AsyncForm' is null or not an object".

总之,它看起来像一个固定尝试,但还没有成型或者我需要做更多的东西。坏消息是,如果它不是那么以后阿贾克斯形式将直至修复完成为大家被打破。

In short it looks like a fix was attempted but it isn't working yet or I need to do something more. The bad news is if it isn't the later then Ajax Forms will be broken for everyone until the fix is complete.

更新:2009-05-07 我收到的反馈意见,从今天微软确认这是一个错误。他们已经记录的缺陷,并表示,他们希望有它固定在将来的版本。

UPDATE: 2009-05-07 I received feedback today from Microsoft confirming that this is a bug. They have logged the defect and said they hope to have it fixed in a future release.

供参考我要离开我的调查,我提交给Microsoft的详细信息。 Appologies为长职位,但也许这将是有益的,任何人试图围绕创建工作。

有在Ajax支持MVC中的一对夫妇的问题。为了说明,考虑在网络上在几个实例中所示的图案:

There are a couple problems in the Ajax support in MVC. To illustrate, consider the pattern illustrated in several examples on the web:

//===========
// View.aspx
//===========
<% using (Ajax.BeginForm("Action", "Controller", 
    new AjaxOptions { UpdateTargetId  = "MyControl", HttpMethod = "POST"}))
{ %>
    <span id="MyControl">
       <% Html.RenderPartial("MyControl"); %>
    </span>
<% } %>

//================
// MyControl.ascx
//================
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<input name="startIndex" type="hidden" value="0" />
<%= Ajax.ActionLink("Prev", "PrevAction",
    new AjaxOptions() { UpdateTargetId="MyControl", HttpMethod="POST"}) %>
<%= Ajax.ActionLink("Next", "NextAction", 
    new AjaxOptions() { UpdateTargetId="MyControl", HttpMethod="POST"}) %>
//...

预计:
这仅仅是一个列表,可以用户可以前后翻页,而无需更新整个页面。

鉴于此设置。我预计2链接标记preV和下一步。点击preV应在控制器后,并在名为在startIndex应该是在请求参数可用的隐藏字段中的值触发prevAction方法。我希望类似的结果单击下一步链接时。

Given this setup. I expect 2 links labeled "Prev" and "Next". Clicking on "Prev" should fire the PrevAction method in the controller as a post and the value in the hidden field named "startIndex" should be available in the request parameters. I expect similar results when clicking the Next link.

实际:
现实情况是,请求对象包含表单参数NONE,即使它表明,它进来作为POST。

Actual:
The reality is that the request object contains NONE of the form parameters even though it shows that it came in as a POST.

为了得到任何使用它们必须通过的ActionLink的,包括参数的变化明确地提供操作链接的参数。当这是所使用的参数成为这有违于使用POST的目的链接的URL的一部分。

In order to get any of the parameters using action link they must be explicitly supplied through the variation of ActionLink that includes parameters. When this is used the parameters become part of the URL of the link which defeats the purpose of having a POST.

那么,为什么是JavaScript错误?
我挖成用于处理提交我贴我的问题,我现在更好地理解为什么它不处理它的例子中的JavaScript code。究其原因似乎与它们连接起来的事件和我所相信的是Internet Explorer中的缺点的方式。

So why is the javascript wrong?
I dug into the javascript code that is used to handle the submit for the example I posted with my question and I now better understand why it doesn't handle it. The reason appears to be related to the way they have wired up events and what I believe is a shortcoming in Internet Explorer.

目前,它的工作方式是,Ajax.BeginForm()辅助类生成与 的onsubmit()函数形式标记拦截表单提交事件。当用户点击提交按钮在的onsubmit()功能火灾和临危参数,其中之一是该事件。

The way it currently works is that the Ajax.BeginForm() helper class generates a form tag with an onsubmit() function to intercept the form submit event. When the user clicks on a submit button the onsubmit() function fires and recieves parameters, one of which is the event.

在MicrosoftMvcAjax脚本查看事件,捆扎,都应该提交,发送请求关闭到服务器的表单属性。问题是,每个WC3标准只的成功的控制都应该公布。在提交按钮的情况下,这是实际点击的按钮。在Internet Explorer中有没有办法确定哪个按钮,实际上造成了提交触发事件,所以脚本只是跳过所有提交按钮。

The MicrosoftMvcAjax scripts look at the event, bundle up the form properties that are supposed to be submitted and sends the request off to the server. The problem is that per WC3 standards only the successful controls are supposed to be posted. In the case of submit buttons this is the button that was actually clicked. Under internet explorer there is no way to determine which button actually caused the submit event to fire so the script just skips all submit buttons.

(在Firefox中的事件包含了一个名为explictOriginalTarget,它指向,实际上导致摆在首位的情况下按钮属性)

请告诉我解决?
微软应该修复它。但是,如果我们需要的东西,迟早我认为唯一的选择就是破解MicrosoftMvcAjax脚本不同的丝打事件。我发现,该表格可以连接到一个处理mouseDown事件,其中的按钮,点击可以保存在一个全局变量中,其中onsubmit处理程序可以将其插入后的参数。

Whats the fix?
Microsoft should be fixing it. However if we need something sooner I believe the only option is to hack the MicrosoftMvcAjax scripts to wire up events differently. I have found that the form can be wired to a handle a mousedown event where the button clicked can be saved in a global variable where the onsubmit handler can insert it into the post parameters.

下面是一些code,我在测试来说明这种技术。我已经证实了它在这两个IE8和Firefox,但我还没有尝试破解它变成了MVC的Ajax脚本尚未...如果我得到更多的时间。我可以在这里发布的结果。

Here is some code that I was testing to illustrate this technique. I have confirmed it works in both IE8 and FireFox but I haven't tried to hack it into the MVC Ajax scripts yet... If I get more time. I may post the results here.

   <script type="text/javascript">
     var _clicked = "";
     function onSubmit(e) {
       var targ;
       if (!e) var e = window.event;
       if (e.target) targ = e.target;
       else if (e.srcElement) targ = e.srcElement;
       if (targ.nodeType == 3) //defeat Safari bug
           targ = targ.parentNode;
       alert("OnSubmit:" + _clicked + " was clicked.");
       return false;
     }
     function Click(e) {
       var targ;
       if (!e) var e = window.event;
       if (e.target) targ = e.target;
       else if (e.srcElement) targ = e.srcElement;
       if (targ.nodeType == 3) //defeat Safari bug
           targ = targ.parentNode;
       _clicked = targ.name;
       return true;
   }


   <form action="/Home/StandardForm" method="post" 
    onsubmit="onSubmit(event)" onmousedown="Click(event)">
      <input type="submit" name="StdPrev" value="StdPrev" />
      <input type="submit" name="StdNext" value="StdNext" />
   </form>

这篇关于ASP.NET MVC Ajax.BeginForm吃提交按钮PARAMS点击。貌似错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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