I am using TreeView and server-side data.
I am trying to add a new node to the tree, with its data values populated from the server.
Here is my Razor code:
@Html.EJS().TreeView("mytree").LoadOnDemand(true).NodeTemplate("#treeTemplate").Fields(field => field.Id("ID") .ParentID("ParentID") .Selected("selected") .Expanded("expanded") .Text("Name") .HasChildren("hasChild") .DataSource(ds => { ds.Url(Url.Action("TreeData")) .Adaptor("UrlAdaptor") .InsertUrl(Url.Action("TreeInsert")) .UpdateUrl(Url.Action("TreeUpdate")) .RemoveUrl(Url.Action("TreeRemove")) .Offline(false) .CrossDomain(true); }) ).Render()
Javscript being called when Adding a new node to nodeid # 99 (using Context Menu handler)
item = { ID:0, parentId = 99, Name = "test" };
treeObj.addNodes([item], parentId, false);
partial C# code:
public ActionResult TreeData(int id, DataManagerRequest dm) {
return Json(treedata);
}
public ActionResult TreeInsert(CRUDModel crud) {
var newNode = new Node();
newNode.ParentID = crud.Value.ParentID;
// set some values here, like parentId, Name, etc.
dataContext.Nodes.Add(newNode);
dataContext.SaveChanges;
// After SaveChanges, newNode will have its ID property set to SQL Identity value (auto-incremented)
return Json(newNode);
}
The automatic CRUD insert call is successful.
TreeView addNodes() -> /TreeInsert payload:
{"value":{"ID":0,"Type":2,"ParentID":99,"Name":"test","Icon":"icon-chat"},"action":"insert"}
The server responds with the full node data needed to set the values in the node, including the new node ID index value.
Response:
{"ID":1005,"ParentID":99,"Name":"New Text","Type":2,"Icon":"icon-chat"}
However the Tree does NOT refresh the node values to set the custom data properties.
The new node is added to the Tree, but still displays "test" as its name, and the ID is still zero. (not the values returned from the Insert call)