Asp.net MVC - 模型绑定图像按钮

发布于 2024-08-31 11:52:29 字数 1046 浏览 4 评论 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 中不起作用,因为它在所有其他浏览器中都适用,并且正在传递正确的数据

I've got a very complex form and i'm using the MVC model binding to capture all the information

I've got it set up to capture all the different submissions that can happen as there are about 10 different submit buttons on the form, and there are also 2 image buttons

I tried to get a bit clever (or so i thought) with capturing the image button submissions, and have created a child class so that i can capture the x value that's returned

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

The parent class looks something like this

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

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

The html for the image button then looks like

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

This works fine in all browsers except for Chrome.

When i debug it in chrome, the Request.Form object contains the values that i would expect, but after the model binding has occurred, the ImageButton property on the ViewDataObject has been set to null

The only difference that i can see between the submission values is that Chrome passes the x as lower case (ViewDataObject.ImageButton.x) and IE passes it as upper case (ViewDataObject.ImageButton.X) but i didn't think that model binding took any notice of casing on property names

Does anyone have any ideas ?

EDIT => Just to clarify, i'm not trying to find a workaround for this, i'm trying to figure out why this technique doesn't work in Chrome considering it works in all the other browsers and the correct data is being passed through

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲影 2024-09-07 11:52:29

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

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

  2. 我认为这个选项是正确的,如下所示。正确分隔您的表格。您可以在一个页面上有多个表单,并将每个表单绑定到它自己的控制器操作,您不需要做难看的 if 语句,它实际上很好地分隔了您的操作。

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

There are a couple of options, let me list a view

  1. To check which button was pressed, you could give all the buttons the same names, but different id's. In your FormCollection on your Controller Action, you should be able to get the button that was pressed by doing something like "collection["buttonName"]" and checking it's value.

  2. I think this option is the correct one to go for, and it is as follows. Separate your forms properly. You can have multiple forms on a page and tie each to it's own Controller Action, you don't need to do ugly if statements and it actually separates your actions nicely.

I hope that this answers your question somehow.

十年不长 2024-09-07 11:52:29

我无法重现您的问题,但我不认为图像输入返回值,只有 .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

I could not reproduce your problem, but I don't think that image inputs return a value, only .x and .y. The following works in Chrome, Firefox and IE (sorry about 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文