基于MVC5和Bootstrap的jQuery TreeView

刚接触到的MVC5 和 bootstrap  树图   


控制器代码如下:

public static List<TC_DictionaryInfo> DInfo = new List<TC_DictionaryInfo>();
/// <summary>
/// TreeView视图
/// </summary>
/// <returns></returns>
public ActionResult May(string TypeCode,int parentId)
{
ViewBag.TypeCode = TypeCode;
ViewBag.ParentId = parentId;
return View();
}
[HttpPost]
public ActionResult GetTreeData(string TypeCode,int parentId)
{
List<TC_DictionaryInfo> DInfo = dbll.GetModelList( "TypeCode=" +TypeCode);
return Json(GetChildNodes(0, new NodeModel(){}, DInfo).nodes);
}
///<summary>
/// GetChildNodes方法,此方法使用递归
/// </summary>
/// <param name="parentId"></param>
/// <returns></returns>
public NodeModel GetChildNodes(int parentId,NodeModel childnodestr,List<TC_DictionaryInfo> DInfo)
{
List<TC_DictionaryInfo> DictionaryList = DInfo.Where(e => Convert.ToInt32(e.ParentId) == parentId).ToList();
for (int i = 0; i < DictionaryList.Count; i++)
{
NodeModel NewNode = new NodeModel();
NewNode.DicId = DictionaryList[i].DicId;
NewNode.text = DictionaryList[i].DICName;
NewNode.ParentId = DictionaryList[i].ParentId;
childnodestr.nodes.Add(NewNode);
GetChildNodes(NewNode.DicId, NewNode, DInfo);
}
return childnodestr;
}

view代码:

<script type= "text/javascript" >
var typecode = @ViewBag.TypeCode;
var parentid = @ViewBag.ParentId;
$( function () {
$.ajax({
type: 'Post' ,
url: '/Type/GetTreeData' ,
data:{
TypeCode:typecode,
ParentId:parentid,
},
//data: para,
dataType: 'json' ,
async: false ,
success: function (data) {
var defaultData = eval(data);
//var defaultData = data;
$( '#treeview4' ).treeview({
color: "#428bca" ,
data: defaultData
});
},
error: function (err) {
alert( '不好意思,数据忘记带上了。。。' );
}
});
</scipt>
第二种方法是以jq方法传过去的:个人觉得最好别用,应为那个容易出错,还不好找bug

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值