【问题标题】:Not Get data in jstree在 jstree 中不获取数据
【发布时间】:2013-08-05 06:57:43
【问题描述】:

我正在使用 jstree 在我的项目中创建类别和子类别的菜单。 但我没有得到或没有以 jstree 格式显示我的数据。 那么,有什么问题。? 请帮助我。 谢谢....

我的控制器代码

   [HttpGet]
    public JsonResult GetCatList()
    {
        IEnumerable<Category> cats = _CategoryBusiness.Select();
        return new JsonResult
        {
            JsonRequestBehavior = JsonRequestBehavior.AllowGet,
            Data = cats
        };
    }

我的模型类

[Serializable]
public class Category
{
    [Key]
    [DisplayName("Id")]
    public int CategoryID { get; set; }

    public string CategoryName { get; set; }

    public int? SubCategoryID { get; set; }
    [ForeignKey("SubCategoryID")]
    public virtual Category SubCategory { get; set; }
}

而 _CategoryBusiness.Select() 是

public List<Category> Select(int id = 0)
    {
        var selectFrom = _Ctx.Categories.Select(a => a);
        var query = selectFrom.Select(a => a);

        if(id > 0)
            query = query.Where(a => a.CategoryID == id);


        return query.ToList();
    }

而我的查看页面代码是

<script type="text/javascript">
$(function () {
    FillJSTree();
});
function FillJSTree() {
    $("#onflycheckboxes").jstree({
        json_data: {
            "ajax": {
                "url": "/Categories/GetCatList/",
                "type": "GET",
                "dataType": "json",
                "contentType": "application/json charset=utf-8",
            }
        },
        //checkbox: {
        //    real_checkboxes: true,
        //    checked_parent_open: true
        //},
        plugins: ["themes", "json_data", "ui"]
    });
}
</script>

。 . . . ..

  <div id="onflycheckboxes">
  </div>

【问题讨论】:

  • 1 - 你有没有试过直接打开这个地址/Categories/GetCatList/来检查视图是否返回数据?

标签: asp.net-mvc json jquery jstree


【解决方案1】:

试试这个:在发送数据到视图之前,序列化它。这对我有用。附:您的类别数据必须是树节点。

[HttpGet]
public string GetCatList()
{
    IEnumerable<Category> cats = _CategoryBusiness.Select();
    string myjsonmodel = new JavaScriptSerializer().Serialize(cats);
    return myjsonmodel;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-21
    • 2015-11-10
    • 2011-04-30
    • 1970-01-01
    相关资源
    最近更新 更多