0

Given the following two buttons:

<button type="submit" name="MyButton" value="Foo">Do Foo</button>
<button type="submit" name="MyButton" value="Bar">Do Bar</button>

When clicking these buttons, all browsers except IE7 and below will post the button's value ("Foo" or "Bar"), whereas IE7 and below instead post the text ("Do Foo" or "Do Bar").

(This is an MVC project, but the issue is not specific to MVC.)

This thread has a lot of answers, but none of them will work when:

  • The value and text are different, and
  • JavaScript is disabled

We want to support flexible button text so that business can change these through our CMS without a code change. So we can't assume that our text and values will be the same. But we also don't want to depend on JavaScript to solve this.

However, I can't think of any way to solve this without either requiring JavaScript, or keeping the value and text the same.

Community
  • 1
  • 1
Jerad Rose
  • 15,235
  • 18
  • 82
  • 153

1 Answers1

0

The usual hack is to key off the name instead of the value.

<button type="submit" name="MyButton_foo" value="Foo">Do Foo</button>
<button type="submit" name="MyButton_bar" value="Bar">Do Bar</button>

Then search the submitted form values for ones that match the pattern /^MyButton_(.*)$/. There are some examples for a variety of languages (although not C#) in an article I wrote some years ago.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335