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 を除くすべてのブラウザーで正常に機能します。

クロムでデバッグすると、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

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.

于 2010-05-06T11:15:16.830 に答える