2016-02-19 68 views
1

我接收該數據並轉化爲json格式後,如下所示:爲什麼treeview控件不能正確加載數據?

[{"id":"2","text":"ALCOHOL CONSUMPTION","parent":"1"},{"id":"9","text":"MARITAL STATUS","parent":"1"},{"id":"13","text":"Recreational drug use","parent":"1"},{"id":"14","text":"OCCUPATION","parent":"1"},{"id":"16","text":" 1 child","parent":"1"},{"id":"17","text":" 2/3/4/5/6 children","parent":"1"},{"id":"18","text":"Activities","parent":"1"},{"id":"27","text":"Tobacco Use","parent":"1"},{"id":"37","text":"SocHx Template","parent":"1"}] 

目前上述json數據是在視圖

@{  
    var j2 = JsonConvert.SerializeObject(allparents);  
} 

j2具有上述jsonjson數據可以作爲expected.When我想未來的數據綁定到jstree控制它不會添加節點正確

這裏是我的jQuery代碼綁定數據

@section Scripts{ 
    <script src="Scripts/jstree.min.js"></script> 
    <script> 

     $(document).ready(function() { 
      $('#Div_jstree').jstree(
         { 
          "json_data": { "data": '@j2' }, 
          "plugins": ["themes", "json_data"] 
         } 
        ); 
     }); 


    </script> 
} 


//the below is the generated HTML copied from chrome elements option 
<table> 
    <tbody><tr> 
     <td> 
      <div id="Div_jstree" class="jstree jstree-1 jstree-default jstree-leaf" role="tree" aria-multiselectable="true" tabindex="0" aria-activedescendant="j1_loading" aria-busy="false"><ul class="jstree-container-ul jstree-children" role="group"></ul></div> 
     </td> 
     <td> 
      <div id="Div_txta"></div> 
     </td> 
     <td> 
      <label id="utctime"></label> 
     </td> 
    </tr> 
</tbody></table> 

,你只能看到一些地方jstree控件已加載,但我的數據在哪裏?

+0

這是什麼網頁的HTML源代碼渲染後?有沒有可能剃刀分析器不夠聰明,以攔截你所擁有的「@ j2」,並且保持原樣? –

回答

0

很可能你的問題是你需要在你的json變量上使用Html.Raw()。在本地進行測試,而無需Raw引號正在格式不正確的:在下面的

<script> 

    $(document).ready(function() { 
     var myObj = 
     { 
     "json_data": { "data": '@Html.Raw(j2)' }, 
     "json_data2": { "data": '@j2' }, 
     "plugins": ["themes", "json_data"] 
     }; 

     alert(myObj); 

    }); 


</script> 

結果,當你在渲染之後查看HTML源:

$(document).ready(function() { 
     var myObj = 
     { 
     "json_data": { "data": '[{"id":"2","text":"ALCOHOL CONSUMPTION","parent":"1"},{"id":"9","text":"MARITAL STATUS","parent":"1"},{"id":"13","text":"Recreational drug use","parent":"1"},{"id":"14","text":"OCCUPATION","parent":"1"},{"id":"16","text":" 1 child","parent":"1"},{"id":"17","text":" 2/3/4/5/6 children","parent":"1"},{"id":"18","text":"Activities","parent":"1"},{"id":"27","text":"Tobacco Use","parent":"1"},{"id":"37","text":"SocHx Template","parent":"1"}]' }, 
     "json_data2": { "data": '[{&quot;id&quot;:&quot;2&quot;,&quot;text&quot;:&quot;ALCOHOL CONSUMPTION&quot;,&quot;parent&quot;:&quot;1&quot;},{&quot;id&quot;:&quot;9&quot;,&quot;text&quot;:&quot;MARITAL STATUS&quot;,&quot;parent&quot;:&quot;1&quot;},{&quot;id&quot;:&quot;13&quot;,&quot;text&quot;:&quot;Recreational drug use&quot;,&quot;parent&quot;:&quot;1&quot;},{&quot;id&quot;:&quot;14&quot;,&quot;text&quot;:&quot;OCCUPATION&quot;,&quot;parent&quot;:&quot;1&quot;},{&quot;id&quot;:&quot;16&quot;,&quot;text&quot;:&quot; 1 child&quot;,&quot;parent&quot;:&quot;1&quot;},{&quot;id&quot;:&quot;17&quot;,&quot;text&quot;:&quot; 2/3/4/5/6 children&quot;,&quot;parent&quot;:&quot;1&quot;},{&quot;id&quot;:&quot;18&quot;,&quot;text&quot;:&quot;Activities&quot;,&quot;parent&quot;:&quot;1&quot;},{&quot;id&quot;:&quot;27&quot;,&quot;text&quot;:&quot;Tobacco Use&quot;,&quot;parent&quot;:&quot;1&quot;},{&quot;id&quot;:&quot;37&quot;,&quot;text&quot;:&quot;SocHx Template&quot;,&quot;parent&quot;:&quot;1&quot;}]' }, 
     "plugins": ["themes", "json_data"] 
     }; 

; 
相關問題