Hi AbdulHaque,
Refer below code. I have created teh sample using the below article.
Model
public class TreeViewNode
{
public string id { get; set; }
public string parent { get; set; }
public string text { get; set; }
}
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
List<TreeViewNode> nodes = new List<TreeViewNode>();
VehiclesEntities entities = new VehiclesEntities();
//Loop and add the Parent Nodes.
foreach (VehicleType type in entities.VehicleTypes)
{
nodes.Add(new TreeViewNode { id = type.Id.ToString(), parent = "#", text = type.Name });
}
//Loop and add the Child Nodes.
foreach (VehicleSubType subType in entities.VehicleSubTypes)
{
nodes.Add(new TreeViewNode { id = subType.VehicleTypeId.ToString() + "-" + subType.Id.ToString(), parent = subType.VehicleTypeId.ToString(), text = subType.Name });
}
//Serialize to JSON string.
ViewBag.Json = (new JavaScriptSerializer()).Serialize(nodes);
return View();
}
}
View
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div id="jstree">
</div>
<input type="hidden" name="selectedItems" id="selectedItems" />
<input type="button" value="Edit" id="btnEdit" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>
<script type="text/javascript">
$(function () {
$('#jstree').on('changed.jstree', function (e, data) {
var i, j;
var selectedItems = [];
for(i = 0, j = data.selected.length; i < j; i++) {
var id = data.selected[i];
if(id.indexOf('-') != -1){
id = id.split("-")[1];
}
selectedItems.push({
text: data.instance.get_node(data.selected[i]).text,
id: id,
parent: data.node.parents[0]
});
}
$('#selectedItems').val(JSON.stringify(selectedItems));
}).jstree({
"core": {
"themes": {
"variant": "large"
},
"data": @Html.Raw(ViewBag.Json)
},
"checkbox": {
'three_state': true,
'whole_node': true,
'tie_selection': false,
"keep_selected_style": false
},
"plugins": ["wholerow", "checkbox"],
}).bind('loaded.jstree', function (event, data) {
$(this).jstree("open_all");
});
$('#btnEdit').on('click', function () {
var type = [];
type.push("Cars");
var subType = [];
subType.push("Karizma");
subType.push("Scorpio");
subType.push("Avenger");
for (var x = 0; x < type.length; x++) {
$("#jstree").jstree("check_node", $("a:contains(" + type[x] + ")"));
}
for (var x = 0; x < subType.length; x++) {
$("#jstree").jstree("check_node", $("a:contains(" + subType[x] + ")"));
}
});
});
</script>
</body>
</html>
Screenshot