I'd like to pass the contents of two sets of textboxes to an action on my controller using jQuery $.ajax. Code I have so far is as follows (all abbreviated for ease of reading):
In my html:
<div class="venue-holder">
<input class="tb-venue" id="venue" name="venue" readonly="readonly" type="text" value="Esfahan" />
<input class="oldVenue" id="oldvenue" name="oldvenue" type="hidden" value="Shiraz" />
<input class="tb-venue" id="venue" name="venue" readonly="readonly" type="text" value="Tehran" />
<input class="oldVenue" id="oldvenue" name="oldvenue" type="hidden" value="Tabriz" />
</div>
In my controller:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UpdateVenue(string[] venue, string[] oldvenue)
{
// do some stuff...
}
In my jQuery:
$('form#formUpdateVenue').submit(function(event) {
event.preventDefault();
var venue = $('input[name=venue]');
var oldvenue = $('input[name=oldvenue]')
$.ajax({
url: '/Admin/UpdateVenue',
type: 'POST',
dataType: 'json',
data: {venue: venue, oldvenue: oldvenue},
success: alert()
});
The issue:
The jquery shown above does not work. When I submit the webpage 'hangs' and the action on my controller does not get called.
However, if I substitute the data:
portion with either of the following all works fine:
data: venue,
or:
data: old venue,
In other words, I can pass one set of textboxes to my action and by debugging I confirm that I get a string array of the correct values.
However, when I try to pass both I get an error. I have tried all of the below to no avail:
data: {venue: venue, oldvenue: oldvenue},
data: {venue, oldvenue},
How can I pass both arrays?
Solved - updated jQuery based on input from Darin
$('form#formUpdateVenue').submit(function(event) {
event.preventDefault();
var tb = $('input[name=venue]');
var tbVenue = new Array();
tb.each(function() {
tbVenue.push($(this).val());
});
var tbOld = $('input[name=oldvenue]');
var tbOldVenue = new Array();
tbOld.each(function() {
tbOldVenue.push($(this).val());
});
$.ajax({
url: '/Admin/UpdateVenue',
type: 'POST',
dataType: 'json',
data: { venue: tbVenue, oldvenue: tbOldVenue },
traditional: true,
success: alert('Ok')
});
});