刚接触到的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