Andrew Kilburn
Andrew Kilburn

Reputation: 2251

How to append JQuery to route values on an Action Link?

At the moment I have an action link which submits data to an action. It also calls a function which populated some JQuery parameters based on data on the view. I would like to append these JQuery values to the action link so they can get submitted at the same time.

View:

 <div class="AltFunctions">
                    <ul>
                        <li>
                            @Html.ActionLink("fd", "ExportClaimBySupplier", "Search", new {ClientID = Model.inputParameters[0], SupplierID = Model.inputParameters[1], JsonReviewPeriod = Json.Encode(Model.inputReviewPeriodIDs), GroupID = Model.inputParameters[2], AmountFrom = JQueryValue, AmountTo = JQueryValue, AllDDValues = JQueryValue}, new { @id = "altExportButton", @class = "AltButton", Title = " Export data to excel" })
                        </li>
                        <li>
                            <a href="#ClaimsBySupplierModal" data-target="#ClaimsBySupplierModal" data-toggle="modal" class="AltButton" id="altInfoButton" title="Information on the Supplier report">Info</a>
                        </li>
                    </ul>
                </div>

JQuery:

<script>
  $('#altExportButton').on('click', function () {
            var AmountFrom = $('#txtAmountFrom').val();
            var AmountTo = $('#txtAmountTo').val();
            var AllDDValues;
            var count = 1;
            $('.ExportValues').each(function () {

                if (count == 1) {
                    AllDDValues = this.value;
                    count = 2;
                }
                else if(count == 2 && AllDDValues != "") {
                    AllDDValues = AllDDValues + ", " + this.value;
                }
            });            
        });

    </script>

I would need to append AmountFrom, AmountTo and AllDDValues to the action link.

Upvotes: 1

Views: 936

Answers (1)

Blake
Blake

Reputation: 641

Action links just become <a> after the server is done with it. Jquery will happen on the client which is after it has transformed into a <a>.

Then you can do something like this:

$('#altExportButton').attr('href', function(index, attr) {
return attr + '&AllDDValues=' + AllDDValues + '&AmountTo =' + AmountTo +'&AmountFrom=' + AmountFrom;
}); 

This should append the values you're looking for to the URL which then should post them to the server on click.

Upvotes: 2

Related Questions