MVC中ViewData中数据转化成json形式的变量的方法

     public ActionResult DataQuery()
        {          
            using (var query = new QueryBLL.QueryClient())
            {
                var x = query.DataQuery(userid);
                var parent =
                    (from a in x.Values select (new { parentid = a.ResourceType.Name, parentname = a.ResourceType.DisPlayName }))
                        .Distinct().ToList();
                var child = (from b in x.Values
                             select (new { parentid = b.ResourceType.Name, childid = b.Name, childname = b.DisPlayName })).ToList();
                ViewData["parent"] = CHINAZTT.ZTAM.UI.Controllers.common.toJSON(parent, "data");
                ViewData["child"] = CHINAZTT.ZTAM.UI.Controllers.common.toJSON(child, "data");
            }

            return View();
        }

 

 

<asp:Content ID="Content2" ContentPlaceHolderID="Header" runat="server">
    <%if (ViewData["parent"] == null || (ViewData["parent"].ToString().Length < 5))
          return;
      else
      {
          Response.Write(@"<script type=""text/javascript"">");
          Response.Write(" var parent=" + ViewData["parent"] + ";");
          Response.Write(" var child=" + ViewData["child"] + ";");
          Response.Write("</script>");
      }
    %>
    <style type="text/css">
        .style2
        {
            height: 22px;
        }
    </style>
</asp:Content>

posted @ 2011-09-19 17:10  风一样的大叔  阅读(473)  评论(0编辑  收藏  举报