Rita
Rita

Reputation: 735

How to pass local variable from Jquery function to ASP.NET MVC controller?

I have a local variable called mode in JQuery function. Based on the radiobutton selection, this variable value is set in JQuery function.

Now i want to access this value in COntroller? How can we do this.

$(':radio').click(function() { var mode = this.value; });

Now How can I access this mode variable in my controller method.

[HttpPost] public ViewResult ExportToExcel(string mode) { }

Appreciate your responses.

Thanks

Upvotes: 0

Views: 1679

Answers (4)

philwilks
philwilks

Reputation: 669

Have you thought about creating a hidden field on the page and then updating its value from jQuery? You can then just read in this value as an additional POST field when the page is submitted to the server.

Upvotes: 2

Naveed
Naveed

Reputation: 123

You can do something like (or any other ajax method you want to call there); $.post('yourController/ExportToExcel/'+mode,.../the rest of the params/);

in your anonymous function to make it like;

$(':radio').click(function() { var mode = this.value; $.post('yourController/ExportToExcel/'+mode,.../the rest of the params/); });

Upvotes: 0

John Boker
John Boker

Reputation: 83709

because mode is declared inside the anonymous function, it cannot be access outside of it. you can create a callback that uses the mode variable, or make it more global so you can access it outside the function.

Upvotes: 0

Yaroslav
Yaroslav

Reputation: 2736

You should perform either synchronous request (form submit with mode variable set) or AJAX POST request, depends on your application design

Upvotes: 1

Related Questions