如何使用 MVC 通过 Ajax 加载 Dynatree

发布于 2024-12-22 08:28:05 字数 1720 浏览 5 评论 0原文

请有人帮忙。 我无法弄清楚如何从服务器加载 Dynatree。 我没有得到一棵树,而是得到“加载错误!(错误)” 我已经阅读了文档的每个部分,并阅读了数百个 stackoverflow 类型的答案,但仍然不明白,所以我希望有人能告诉我该怎么做。

这就是我所拥有的:

VIEW

@{
  Layout = null;
}
<!DOCTYPE html>
<html>
<head>
  <title>LoadAjax</title>
  <script src="../../Scripts/jquery-1.7.1.js" type="text/javascript"></script>
  <script src="../../Scripts/jquery-ui-1.8.16.js" type="text/javascript"></script>
  <script src='../../Scripts/jquery.cookie.js' type="text/javascript"></script>
  <link rel='stylesheet' type='text/css' href='../../Content/skin/ui.dynatree.css' />
  <script src='../../Scripts/jquery.dynatree.js' type="text/javascript"></script>
  <script type="text/javascript">
    $(function () {
      $("#tree").dynatree({
        initAjax: { url: "/LoadAjax/GetNodes" }
      });

    });    
  </script>
</head>
<body>
  <div id="tree"></div>
</body>
</html>

CONTROLLER

  public ActionResult GetNodes()
    {
      var n1 = new DynaNode { title = "Node 1", key = "k1", isLazy = false };
      var n2 = new DynaNode { title = "Node 2", key = "k2", isLazy = false };
      var n3 = new DynaNode { title = "Node 3", key = "k3", isLazy = false };
      var nodeArray = new List<DynaNode> {n1, n2, n3};
      return Json(nodeArray);
    }

CLASS

  public class DynaNode
  {
    public string title { get; set; }
    public bool isFolder { get; set; }
    public bool isLazy { get; set; }
    public string key { get; set; } 
  }

Please someone help.
I cannot work out how to load a Dynatree from the server.
Instead of getting a tree I get "Load error! (error)"
I have read every single part of the documentaion and read hundreds of stackoverflow type answers and still do not understand, so I am hoping someone will just tell me what to do.

This is what I have:

VIEW

@{
  Layout = null;
}
<!DOCTYPE html>
<html>
<head>
  <title>LoadAjax</title>
  <script src="../../Scripts/jquery-1.7.1.js" type="text/javascript"></script>
  <script src="../../Scripts/jquery-ui-1.8.16.js" type="text/javascript"></script>
  <script src='../../Scripts/jquery.cookie.js' type="text/javascript"></script>
  <link rel='stylesheet' type='text/css' href='../../Content/skin/ui.dynatree.css' />
  <script src='../../Scripts/jquery.dynatree.js' type="text/javascript"></script>
  <script type="text/javascript">
    $(function () {
      $("#tree").dynatree({
        initAjax: { url: "/LoadAjax/GetNodes" }
      });

    });    
  </script>
</head>
<body>
  <div id="tree"></div>
</body>
</html>

CONTROLLER

  public ActionResult GetNodes()
    {
      var n1 = new DynaNode { title = "Node 1", key = "k1", isLazy = false };
      var n2 = new DynaNode { title = "Node 2", key = "k2", isLazy = false };
      var n3 = new DynaNode { title = "Node 3", key = "k3", isLazy = false };
      var nodeArray = new List<DynaNode> {n1, n2, n3};
      return Json(nodeArray);
    }

CLASS

  public class DynaNode
  {
    public string title { get; set; }
    public bool isFolder { get; set; }
    public bool isLazy { get; set; }
    public string key { get; set; } 
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫南 2024-12-29 08:28:05

好吧,我发现问题了。我很尴尬地承认这是一个初学者的错误。

查看

initAjax: {
  type: "POST", // This was needed
   url: "/DynaTree/GetNodes"
}

控制器

[HttpPost] // This was needed
public JsonResult GetNodes(string key)

Ok, I found the problem. I am embarrased to admit that it was a beginner's mistake.

VIEW

initAjax: {
  type: "POST", // This was needed
   url: "/DynaTree/GetNodes"
}

CONTROLLER

[HttpPost] // This was needed
public JsonResult GetNodes(string key)
ぶ宁プ宁ぶ 2024-12-29 08:28:05

请运行此解决方案:

return Json(nodeArray, JsonRequestBehavior.AllowGet);

Run this solution, please:

return Json(nodeArray, JsonRequestBehavior.AllowGet);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文