Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding additional parameter to form submit

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?

like image 316
Sachin Kainth Avatar asked Oct 31 '12 16:10

Sachin Kainth


People also ask

How do I add another field to a form before submitting?

var form = addDataToForm('myFormId', { 'uri': window. location. href, 'kpi_val': 150, //... }); you can add # if you like too ("#myformid").

How do you submit form only once after multiple clicking on submit?

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.

What does JavaScript submit () do?

submit() allows to initiate form sending from JavaScript. We can use it to dynamically create and send our own forms to server.

Can you submit form using JavaScript?

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.


2 Answers

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(); }); 
like image 161
Jamiec Avatar answered Sep 19 '22 06:09

Jamiec


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" })){ 
like image 29
yardpenalty.com Avatar answered Sep 18 '22 06:09

yardpenalty.com