0

我有一个非常复杂的表单,我正在使用 MVC 模型绑定来捕获所有信息

我已经将它设置为捕获所有可能发生的不同提交,因为表单上有大约 10 个不同的提交按钮,还有 2 个图像按钮

我试图通过捕获图像按钮提交变得有点聪明(或者我认为),并创建了一个子类,以便我可以捕获返回的 x 值

public class ImageButtonViewData {
    public int x { get; set; }
    public string Value { get; set; }
}

父类看起来像这样

public class ViewDataObject {
    public ImageButtonViewData ImageButton { get; set; }

    public ViewDataObject(){
        this.ImageButton = new ImageButton();
    }
}

图像按钮的 html 然后看起来像

<input type="image" id="ViewDataObject_ImageButton" name="ViewDataObject.ImageButton" />

这适用于除 Chrome 之外的所有浏览器。

当我在 chrome 中调试它时,Request.Form 对象包含我期望的值,但是在模型绑定发生后,ViewDataObject 上的 ImageButton 属性已设置为 null

我可以看到提交值之间的唯一区别是 Chrome 将 x 传递为小写 (ViewDataObject.ImageButton.x) 而 IE 将其传递为大写 (ViewDataObject.ImageButton.X) 但我不认为模型绑定注意到属性名称上的大小写

有没有人有任何想法?

编辑 => 只是为了澄清,我不是想找到解决方法,我想弄清楚为什么这种技术在 Chrome 中不起作用,因为它适用于所有其他浏览器并且正在传递正确的数据通过

4

2 回答 2

0

我无法重现您的问题,但我认为图像输入不会返回值,只有 .x 和 .y。以下适用于 Chrome、Firefox 和 IE(对不起 vb.net):

Public Class ImageButtonViewData
    Public Property X As String
    Public Property Y As String

    Public ReadOnly Property WasClicked As Boolean
        Get
            Return Not String.IsNullOrEmpty(X)
        End Get
    End Property

End Class
于 2011-06-22T11:32:44.390 回答
0

有几个选项,让我列出一个视图

  1. 要检查按下了哪个按钮,您可以为所有按钮赋予相同的名称,但使用不同的 ID。在 Controller Action 上的 FormCollection 中,您应该能够通过执行“collection [”buttonName“]”之类的操作并检查其值来获取按下的按钮。

  2. 我认为这个选项是正确的选择,如下所示。正确分开您的表格。您可以在一个页面上有多个表单,并将每个表单与它自己的控制器动作联系起来,您不需要做丑陋的 if 语句,它实际上很好地分离了您的动作。

我希望这能以某种方式回答你的问题。

于 2010-05-06T11:15:16.830 回答