views:

1149

answers:

4

I have a HTML form, and I have a Controller Action that accepts the POST request. Everything works with a regular submit button, but I would like to submit the form with a link (<a>-tag) instead, to be able to further control the formatting. Is there any way of doing this nicely built into the ASP.NET MVC Framework, or should I write my own extension method? Is it even possible to do this without javascript (I will use AJAX in the future, but it has to work without).

A: 

You should simulate something like __doPostBack in Web forms in Javascript like this:

<form method="POST" action="URL" id="myForm">
   <input type="hidden" id="myInput" />
</form>
<script type="text/javascript">
   function postback(data) {
       document.getElementById("myInput").value = data;
       document.getElementById("myForm").submit();
   }
</script>

And then define the link as:

<a href="javascript:postback('some data')">click here</a>
Mehrdad Afshari
this does solve the problem for js-enabled browsers. what happens to the ones that do not have js enabled?i suppose i could use a <noscript>-tag with a submit button, but then both the button and the link would be visible... any ideas?
Tomas Lycken
There is no way you could submit a form with an <a> tag without JS. MS itself, uses this solution for Web forms.
Mehrdad Afshari
A: 

I'm not aware of a helper and as far as I know it is impossible to submit a form using an anchor tag without using javascript.

Garry Shutler
+1  A: 

Here is a complete example. Note that this particular example does something fairly important: it has a fallback for browsers with JavaScript disabled.

Craig Stuntz
A: 

You cannot 'submit a form' using a link (<a> tag) without Javascript. The javascript is going to generate a standard POST request (same as clicking a submit form button) behind the scenes.

There are other workarounds for those with JS disabled, look at what @Craig Stuntz submitted.

Redbeard 0x0A