2012-09-05 29 views
0

在create.jstree上的.bind中的$ .ajax調用中,一切似乎都正確。但是,對控制器的調用(我正在使用MVC 3)並未出於某種原因。我在由ajax調用引用的POST函數上放置了一個斷點,但它甚至不會被調用。這導致我相信bind和ajax調用的組合出現問題,從而阻止post函數被調用。我會很感激這個建議。JSTree中的Ajax調用綁定在「create.jstree」上下文菜單不起作用

jstree代碼:

 $("#RequirementsTree") 
    .bind("select_node.jstree", function(event, data) { 
      if(is_requirement_node(data)) 
      { 
       var id = data.rslt.obj.attr("id"); 

       if(id != null) 
       { 
        $("#RequirementsTree").jstree('close_all') 
       } 
       else { 
        alert("Requirement node select error"); 
       } 
      } 
    }) 
    .bind("create.jstree", function(e, data) { 
     alert(data.rslt.obj.text()); 
     alert(ParentNode); 
     // Ajax call to Server with parent node id and new node text 
     debugger; 
     $.ajax({ 
      type: "POST", 
      url: function(node) { 
       return "/RMS/insertRequirementNode"; 
      }, 
      data: { 
        ParentID : ParentNode, 
        ChildNodeText : data.rslt.obj.text() 
      }, 
      success: function(new_data) { 
       return new_data; 
      } 
     }); 
     ParentNode = null; 
     if (data.rslt.parent == -1) { 
      alert("Can not create new root directory"); 
      // Rollback/delete the newly created node 
      $.jstree.rollback(data.rlbk); 
      return; 
     } 
       BranchReqFLag = null; 
    }).jstree({ 
     json_data: { 
      data: RBSTreeModel, 
      ajax: { 
       type: "POST", 
       data: function (n) { 
        return { 
         NodeID: n.attr("id").substring(4), 
         Level: n.attr("name").substring(7) 
        }; 
       }, 
       url: function (node) { 
        return "/Audit/GetRequirementsTreeStructure"; 
       }, 
       success: function (new_data) { 
        return new_data; 
       } 
      } 
     }, 
     contextmenu: { 
      items: function($node) { 
        return { 
         createItem : { 
          "label" : "Create New Branch", 
          "action" : function(obj) { this.create(obj); BranchReqFlag = "Branch"; ParentNode = obj.attr("id").substring(4);} 
         }, 
         renameItem : { 
          "label" : "Rename Branch", 
          "action" : function(obj) { this.rename(obj);} 
         } 
        }; 
      } 
     }, 
     plugins: ["themes", "json_data", "ui", "crrm", "contextmenu"] 
    }); 

控制器POST功能:

 [AllowAnonymous] 
    [HttpPost] 
    public int insertRequirementNode(int ParentID, string ChildNodeText) 
    { 
     RBSText CurrNode = new RBSText(); 
     int CurrentNodeID = -1; 

     //CurrNode.RMSHierarchyText = ChildNodeText; 
     using (Contract ActiveContract = getContract()) 
     { 
      try 
      { 
       // Inserts the new node beneath the Parent Node 
       CurrentNodeID = ActiveContract.CreateRMSNode(CurrNode, ActiveContract.ContractId, ActiveContract.user_id, 2, "Child"); 
      } 
      catch (Exception ex) 
      { 
       throw ex; 
      } 
     } 
     return CurrentNodeID; 
    } 

回答

2

$.ajax()功能指定的URL可能是錯誤的。使用@Url.Content()以及服務器根通配符:~爲您的URL添加前綴,因此無論您的應用程序如何部署,您的URL都始終引用正確的服務器根路徑,假設您在Razor View中設置了jstree,有機會獲得剃刀引擎:

$.ajax({ 
     type: "POST", 
     url: "@Url.Content("~/RMS/insertRequirementNode")", 
     data: { 
       ParentID : ParentNode, 
       ChildNodeText : data.rslt.obj.text() 
     }, 
     success: function(new_data) { 
      return new_data; 
     } 
    }); 

如果您在.js文件中設置jstree,那麼你就需要在服務器根目錄存儲在Razor視圖定義第一個javascript變量,指的是變量,而不是。

+0

你實際上擊中了頭部的指甲。在我檢查你的答案之前,我想到了這一點。我實際上使用了@ Url.Action(「〜RMS ...」),它像一個魅力。感謝你的回答! – TheDude