0

I have a piece of javascript that posts to an action method, and with it some data. This data is in the form:

pageID  1
photos[1]   2
photos[2]   1

My Action Method looks as follows:

public ActionResult PosImage(IEnumerable<int> photos, int pageID)

Naturally, pageID comes through in the form data, but photos is always null.

Is it possible for ASP.MVC to bind like this?

The JS that handles the post looks as follows:

var ids = { 'photos': {}, 'pageID': SitePhotos.site_id };
    $('#stone-photo-sortable li').each(function (i) {
        i++;
        var id = $(this).attr('rel');
        ids.photos[id] = i;
    });
    $.ajax({
        type: "POST",
        url: '/backend/site/posimage/?timestamp=' + new Date().getTime(),
        dataType: 'json',
        data: ids,
        beforeSend: function () { },
        complete: function () { },
        success: function (html) {
            if (html.worked == 1) {
                Notify.add('success', 'New Positions saved to the database..');
            } else {
                Notify.add('failure', 'Couldn\'t save positions.');
            }

        }
    });

3 Answers 3

1

I havent done it from javascript, but your post data should actually be like this:

photos: 1
photos: 2
photos: 3

You probably need to use a different $.ajax overload inorder to pass post data with duplicate key names.

edit seems like all you have to do is to set traditional flag to true.

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

Comments

1

It seems to me this would work better for you if you turn the photos object around and making it into an array instead like this:

var ids = { 'photos': [], 'pageID': SitePhotos.site_id };
    $('#stone-photo-sortable li').each(function (i) {
        ids.photos[i] = $(this).attr('rel');
    });

Comments

0

Sergio, this can be fixed very easy.

IEnumerable<int> photos will work, but your array should always start at 0! not at 1

make it start at 0 and MVC will parse it into your IEnumerable

read more about it here: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

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.