开发者

Post array to ASP.NET MVC 2 Controller using AJAX

开发者 https://www.devze.com 2023-01-18 05:28 出处:网络
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.

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" va开发者_JAVA技巧lue="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) + "]");
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消