This is a migrated thread and some comments may be shown as answers.

[Solved] Sending TreeViewItem to Controller using Ajax

1 Answer 50 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Sukhvinder Pal
Top achievements
Rank 1
Sukhvinder Pal asked on 26 Apr 2012, 07:58 AM
I have the following code in my script, which tries to add child asynchronously.
I want to send the TreeviewItem and the name of the new node to the controller action , How should I proceed?
function AddCategory() {
    var treeViewElement = getTreeView();
    var treeView = getTreeViewData(treeViewElement);
    var selected = getSelected(treeViewElement);
     
    // Add the category asynchronously.
    $.ajax({
        type: "POST",
        url: "/Controller/Action"
        data: {parent:  },
        dataType: "json", // Not sure how to send data to controller action
     // with prototype: ActionResult Action(TreeviewItem parent, string newNodeName)
    })
 
    // Rebind the parent node by fetching children.
    treeView.ajaxRequest(selected);
}

1 Answer, 1 is accepted

Sort by
0
Sukhvinder Pal
Top achievements
Rank 1
answered on 29 Apr 2012, 11:14 AM
I used sending the id of the selected node as below instead of trying to send the treeviewItem. But on success, if the child node was added to the root the tree does not display this new node, I need to refresh the page. I tried doing a lot of things but the child added to the root does not show, see code below

function AddCategory() {
    var treeViewElement = getTreeView();
    var treeView = getTreeViewData(treeViewElement);
    var itemData = { Text: $("#itemText").val() };
    var selected = getSelected(treeViewElement);
    var url = treeViewElement.attr("data-treeview-addcategory");
    var parentId = selected.length != 0 ? treeView.getItemValue(selected) : "0";
     
    // Add the category asynchronously.
    $.ajax({
        type: "POST",
        url: url,
        data: {
            parentId: parentId,
            categoryName: itemData.Text
        }
    }).done(function (data) {
        // Rebind the parent node by fetching children.
        if (selected.length != 0) {
            treeView.ajaxRequest(selected);
        }
        else {
            var newNode = treeView.append(data.Text);
             
            newNode.bind(data);
            treeView.reload();
        }
    });
}
Tags
TreeView
Asked by
Sukhvinder Pal
Top achievements
Rank 1
Answers by
Sukhvinder Pal
Top achievements
Rank 1
Share this question
or