views:

48

answers:

2

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')
    });
});
A: 

Remnant,

the 1st thing i notice about the above html in the div, is the duplicate ids (id="venue" - id="oldvenue"). this is going to cause you issues. I think you'd be better sorting that out first. then, take a look in fiddler or firebug and you should see the error that's occurring in your ajax call. My 'guess' is that your 'routes' may also need tweaking in the global.asax file, then your action 'should' (hopefully) fire correctly.

as i said, firebug/fiddler to the rescue perhaps...

[edit] - had a few 'obvious' further thoughts. have you tried the following action signature:

public ActionResult UpdateVenue(FormCollection collection)

and then examine the request as such:

var venue = collection["venue"]; 
var oldVenue = collection["oldvenue"];

etc, etc. see example at:

http://code-inside.de/blog-in/2009/04/06/howto-from-the-view-to-the-controller-in-aspnet-mvc-with-modelbinders/

also, look at the $ajax serialize() method to use in your code:

http://api.jquery.com/serialize/

jim
Jim - Thanks for your input. I'll try out yur collection idea and see how it works. Note that I cannot use serialize() as the texboxes are not within my Html.Beginform...
Remnant
Jim - quick question on the ID point you raise. I understand that each ID should be unique and this is considered best practice. That said, if the only way I am referencing my textBoxes is through jQuery using the name attribute, what is the consequence of having duplicate IDs? My html renders fine and the jQuery does capture the textBox values as expected...
Remnant
remnant - if you're ALWAYS going to reference by name and are going to be the only developer on the project, then i think it's fine to do as you are doing. however, if there's more than one developer involved, then i think you should adopt a set of standards when doing this kind of thing. these should be documented literally just detail the fact that in circumstances such as the above, then it's ok to use duplicate id's. it's really just a matter of adopting a practice and being rigourous with it. i wouldn't feel secure doing it in this way unless it was well understood and documented.
jim
Jim - agree with your comments about setting coding standards, especially within the team. Good point. I am new to programming webpages (just doing a fun personal project to force learning on mvc and jQuery) and just wanted to check that using duplicate IDs wouldn't have consequences that weren't immediately obvious to me e.g. incorrect page rendering, css bugs etc.
Remnant
+3  A: 

Try this:

$.ajax({
    url: '/Admin/UpdateVenue',
    type: 'POST',
    dataType: 'json',
    data: { venue: [ 'elem1', 'elem2' ], oldvenue: [ 'elemA', 'elemB' ] },
    traditional: true,
    success: function(result) {
        alert('ok');
    }
});

Notice that the venue and oldvenue parameters have to be arrays and not strings as in your case. You were simply copying them from the values of an input field which returns a single string. So if you want an array you need to construct a javascript array object.

Also notice the traditional: true config value which is required to successfully perform the model binding if using jquery 1.4 and higher.

Darin Dimitrov
Darin - this makes sense and I'll give it a try. I did try putting my textbox values into an array and passing that to the controller but this did not work for me. Please see updated post for jQuery code I was using...
Remnant
@Darin - based on your input I have solved my issue (see updated post). I put my textbox values into arrays as you suggested. Thanks for taking the time to help me with this issue. Much appreciated.
Remnant