0

I currently have a bunch of methods in my controllers which take selected records from table rows.

So I might have something like

var ids = [];
var prices = [];
var customers = [];

$selectedRow.each(function() {
    ids.push($(this).find('.id').text());
    prices.push($(this).find('.price').text());
    customers.push($(this).find('.customer').text());
});

$.post(....) // AJAX call to controller method

And in the controller I end up with

public ActionResult DoSomething(int[] ids, double[] prices, string[] customers) { ... }

which is just a bit messy to deal with using iterators.

What I'd really like is to have

Class Foo
{
    int id;
    double price;
    string customer;
}

and be able to receive

public ActionResult DoSomething(List<Foo> foos) { ... }

is this possible?

4 Answers 4

3

A bit hacky but here's an example:

// query array: construct this as usual
var array = [{ id: '1', name: 'name 1' }, { id: '2', name: 'name 2'}];

// map the array into an array of DOM hidden fields
var query = $.map(array, function (element, index) {
    return [$(document.createElement('input'))
                    .attr('type', 'hidden')
                    .attr('name', 'foos[' + index + '].id')
                    .val(element.id),
                $(document.createElement('input'))
                    .attr('type', 'hidden')
                    .attr('name', 'foos[' + index + '].name')
                    .val(element.name)
                ];
});

// construct a form
var form = $(document.createElement('form'));
$(query).appendTo(form);

$.ajax({
    url: '<%: Url.Action("Test") %>',
    data: form.serialize(),
    dataType: 'json',
    success: function (result) {
        alert('success');
    }
});

This will successfully bind to a controller action of the form:

public ActionResult Test(IEnumerable<Foo> foos) 
{
    ...    
}

where Foo:

public class Foo
{
    public string Id { get; set; }
    public string Name { get; set; }
}

Remark: All this is not necessary if you configure your controller action to accept JSON. In ASP.NET MVC 3 this is automatically included in the framework.

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

Comments

0

Yes it is.

Look at this question for details.

Also these resources can help you:

2 Comments

I'm not trying to model bind to a form though, those solutions won't work with jQuery submitted values will they?
Yes they will if you use ("#form").serialize() when submitting the form itself
0

Phil Haack wrote a blog post on this too: Model Binding To A List

Also, MVC 3's model binder will support JSON post data I think - but this is obviously not going to help you today :(

Comments

0

In my case, the view is not being returned. Its showing the alert in the call back

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.