1

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) + "]");
    }
1
  • The question is what people think about this solution? Maybe somebody know contraindications. Commented Sep 29, 2010 at 12:25

0

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.