开发者

Problem reading List Collection in ASP.net MVC View through Json

开发者 https://www.devze.com 2023-01-02 12:47 出处:网络
Whenev开发者_运维百科er i return a list collection from a controller through Json. Im unable to get that list but if i just return a string from controller its working fine.

Whenev开发者_运维百科er i return a list collection from a controller through Json. Im unable to get that list but if i just return a string from controller its working fine. In View i have

    <script type="text/javascript" language="javascript">
        $(function () {
            $('#btnFillList').click(function () {
                alert("btnclick");

                var URL = '<%= Url.Action("JsonFunc2","Customer") %>';
                $.post(URL, null, function (data) {
                    for (var i = 0; i < data.length; i++) {
                    }

                });
            });
        });      
    </script>

<input type="submit" id="btnFillList" value="Load" />

In Controller i have

public ActionResult JsonFunc2()
        {

            var cust = _db.tblCustomers.ToList();
            return Json(cust);
        }


Try returning an array instead of list:

var cust = _db.tblCustomers.ToArray();
return Json(cust);


Try using eval(data) before looping

 $.post(URL, null, function (result) {
       var data = eval('(' + result + ')');
       for (var i = 0; i < data.length; i++) {

       }
0

精彩评论

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

关注公众号