views:

96

answers:

0

Hello. There is known trouble with sending array to ASP.NET MVC controller. I've found a lot of solutions like that. Why don't use usual object instead array? It works good. Example of code:

    <script>
    $(function(){
        $('.asArray').click(function(){
            var array = Array();
            array[0] = 'Dima';
            array[1] = 'Ann';
            array[2] = 'John';
            $.post('/Home/Get', {data: array}, function(data){alert(data);});
        });
        $('.asObject').click(function(){ // works good
            var array = Object();
            array[0] = 'Dima';
            array[1] = 'Ann';
            array[2] = 'John';
            $.post('/Home/Get', {data: array}, function(data){alert(data);});
        });
    });
</script>
<div>
    <input type="button" class="asArray" value="asArray"/>
    <input type="button" class="asObject" value="asObject"/>

controller action:

public ActionResult Get(IEnumerable<string> data)
    {
        if (data == null)
            return Content("data == null");
        return Content("data = [" + data.Aggregate((agr, curr) => agr + ", " + curr) + "]");
    }