I have a form declaration in my Razor view
<form method="post" action="/Lot/LotList?auctionEventId=@auctionEventId" id="filterForm">
(Incidentally, I chose to write it out like this rather than use Html.BeginFrom
because I needed to give it an id and didn't know how to do that with Html.BeginFrom
- but that is not the issue here)
Outside of this form I have a button which submits this form (there is also a submit button in the form)
<input type="button" onclick="$('#filterForm').submit();" value="Show all" />
Now, the issue is that if this button is used to submit the form I want the action in the form to change to
action="/Lot/LotList?auctionEventId=@auctionEventId&showAll=true"
How do I alter the action and pass this additional parameter? Is there a totally better way of doing all this?
var form = addDataToForm('myFormId', { 'uri': window. location. href, 'kpi_val': 150, //... }); you can add # if you like too ("#myformid").
onsubmit = function () { if (allowSubmit) allowSubmit = false; else return false; } })(); (well, as sure-fire as you can get with JS enabled anyway). You could disabled the button as a visual confirmation to the end user that the form can only be submit once too. Very correct.
submit() allows to initiate form sending from JavaScript. We can use it to dynamically create and send our own forms to server.
Generally, a form is submitted when the user presses a submit button. However, sometimes, you may need to submit the form programmatically using JavaScript. JavaScript provides the form object that contains the submit() method. Use the 'id' of the form to get the form object.
Appending query string parameters onto the form action, and trying to change that at runtime is tricky (but not impossible). Far easier is to use hidden fields:
<form method="post" action="/Lot/LotList" id="filterForm"> <input type="hidden" name="auctionEventId" value="@auctionEventId" /> ...
So now all you have to do is add another hidden field for "showAll"
<form method="post" action="/Lot/LotList" id="filterForm"> <input type="hidden" name="auctionEventId" value="@auctionEventId" /> <input type="hidden" name="showAll" value="false" id="showAllField" /> ...
And just hook up a jquery event on your showAll button:
<input id="showAllButton" type="button"/>
jQuery:
$('#showAllButton').click(function(){ $('#showAllField').val("true"); $('#filterForm').submit(); });
I know you said this is not the issue but you can add attributes to Html.BeginForm
like this:
@using (Html.BeginForm("ActionName","ControllerName", FormMethod.Post, new { id = "filterform" })){
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With