MVC - Model binding image button 

حمایت از ما | مشهدهاست MVC - Model binding image button

حمایت از ما | مشهدهاست

حمایت از ما | مشهدهاست حمایت از ما | مشهدهاست

حمایت از ما | مشهدهاست
` MVC - Model binding image button


6 years ago


1084 times


3 years ago

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

2010-05-06 11:06:09Z


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.


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

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 

 اين مطلب بدون دخالت انساني عينا از اين آدرس کپي شده و تمامي مسوليت آن با ناشر اصلي است.

How to Convert DataSet to DataTable
How can I get the CheckBoxList selected values, what I have doesn't seem to work C#.NET/VisualWebPart
Change GridView row color based on condition
What causing this “Invalid length for a Base-64 char array”
How to send HTML-formatted email? [duplicate]
How to solve error message: “Failed to map the path '/'.”

Select method in List<t> Collection
Setting dropdownlist selecteditem programmatically
How to write a simple Html.DropDownListFor()?
What is the right way to populate a DropDownList from a database?
A potentially dangerous Request.Path value was detected from the client (*)
Download/Stream file from URL -