Jonathan Wood
Jonathan Wood

Reputation: 67195

Post Hidden Input Element Values to Controller?

I have a number of hidden input elements on my ASP.NET MVC page. They each hold an integer value. They are identified by $('table#ratings input[name=newReviewRatings]').

Is it possible to post the integers in those hidden input elements using $.post() so that the controller is passed an array of integers?

This is probably easier using a <form> element and simply posting the form. But is it possible to do it using jQuery?

Upvotes: 0

Views: 94

Answers (2)

palaѕн
palaѕн

Reputation: 73896

An ideal fit for this situation is using map like:

var data = $('#ratings input[name=newReviewRatings]').map(function(){
    return $(this).val();
}).get();

// Post the data to the respective url
$.post(your_url, data);

The .map() method is particularly useful for getting or setting the value of a collection of elements.

Upvotes: 2

Orangepill
Orangepill

Reputation: 24645

You should be able to get your array using something like this.

var data = [];
$('table#ratings input[name=newReviewRatings]').each(function(){
    data.push($(this).val());
});
$.post(url, data);

Upvotes: 2

Related Questions