1

I have ajax code that gets data from a radio button.

  $("input[name='SelectedCG']").on('change', function () {
        $.ajax({
            url: '@Url.Action("FilterReports", "Reports")',
            data: { cgName: $("input[name='SelectedCG']:checked").val() },
            type: 'POST',
            success: function (data) {
                $('#reportTable').html(data);
                $('#reportTable').show();
            },
            error: function(xhr, status, error)
            {
                alert(xhr.responseText);
            }
        })
    });

I want to bind this data to a submit button which can pass this information to a method in a controller.

<input type="submit" value="Export" class="btn btn-primary btn-large" onclick="location.href='@Url.Action("Export", "Reports")'">

Any help would be appreciated!

5
  • What are you using a submit button to make a GET? And what information do you want to pass to the Export() method? Commented May 23, 2016 at 0:45
  • So I get a list of names if I click on the radio button, but I want to click on the submit button that can pass this data to Export() method Commented May 23, 2016 at 0:48
  • You don't. You just pass the value of the radio button and get the data again in the Export() method (just as you did in the FilterReports() method. Commented May 23, 2016 at 0:50
  • Is there any code like bind: true in above ajax code to a button with the value export? Commented May 23, 2016 at 0:50
  • And if you want to redirect to a GET method (as opposed to posting) then you need to wrap your radio buttons in a for with FormMethod.Get or else you need to use javascript to generate the url based on the selected radio button value. Commented May 23, 2016 at 0:52

1 Answer 1

1

It is better to pass the value of the radioButton to the Export method and get the data again. In any case you might need to do some more work on that data before you export it any way.

Also you might want to check the user's permissions to export such data.

Also you might not want to transfer such data over the network back and forth. It is easier (faster) to send the value of a button than it is to send a collection of data which has to be exported.

Not to mention that there are also security concerns depending on how you will handle the data you receive and if it is correct or not? What If a user changes it manually in the browser before hitting the export button? Would that hurt your application's logic in some way and would you want to protect yourself against that?

Best is to use an HttpPost, to include AntiForgeryToken, to submit the radio button value and check the user's permissions.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.