2016-05-04 52 views
0

您好我想提出HTTP GET調用返回JSON如下MVC控制器動作:車把不正確填充模板JSON響應

[ 
    { 
     "PlanCode": "P001 ", 
     "PlanName": "Plan1 " 
    }, 
    { 
     "PlanCode": "P002$ ", 
     "PlanName": "Plan2$ " 
    }, 
    { 
     "PlanCode": "P003$ ", 
     "PlanName": "Plan3$ " 
    }, 
    { 
     "PlanCode": "P004$ ", 
     "PlanName": "Plan4$ " 
    }, 
    { 
     "PlanCode": "P005$ ", 
     "PlanName": "Plan5$ " 
    }, 
    { 
     "PlanCode": "P006$ ", 
     "PlanName": "Plan6$ " 
    }, 
    { 
     "PlanCode": "P007$ ", 
     "PlanName": "Plan7$ " 
    }, 
    { 
     "PlanCode": "P007$ ", 
     "PlanName": "Plan7$ " 
    }, 
    { 
     "PlanCode": "P008$ ", 
     "PlanName": "Plan8$ " 
    }, 
    { 
     "PlanCode": "P009$ ", 
     "PlanName": "Plan9$ " 
    } 
] 

使用handlbar代碼如下:

var PLAN_METHOD = { 

    handlerData: function (planJSON) { 

     var templateSource = $("#plan-template").html(); 

     template = Handlebars.compile(templateSource); 

      var context = planJSON; 
      plansHTML = template({planJSON:context}); 


     $('#plans-div').html(plansHTML); 

    }, 
    loadPlansData: function() { 

     $.get("http://localhost:41801/plan/getplans",null,this.handlerData) 

    } 
}; 

$(document).ready(function() { 

    PLAN_METHOD.loadPlansData(); 
}); 

HTML模板如下所示:

<div id="plans-div" > 
</div> 

<script id="plan-template" type="text/x-handlebars-template"> 
    <table > 
     <thead> 
      <tr> 
       <th>Plan Code</th> 
       <th>Plan Name</th> 

      </tr> 
     </thead> 
     <tbody> 
      {{#each Plans}} 
      <tr> 
       <td>{{this.PlanCode}}</td> 
       <td>{{this.PlanName}}</td> 
      </tr> 
      {{/each}} 
     </tbody> 
    </table> 
</script> 

Something'plansHTML'in handlebar javascript code is not getting popu與JSON日期行相關聯。請幫助

+0

其中'plans-div'在您的html?這應該是一個ID。 – alsafoo

+0

抱歉不知何故沒有添加上面的計劃div的HTML。它在那裏如下:

但仍然不知何故'的HTMLHTML'在手柄JavaScript代碼沒有得到與JSON數據行填充。 – Pam

+0

你能否更新你的問題以包含'plans-div' – alsafoo

回答

0

您的模板正在尋找數據的屬性Plans的數組。但是您的數據沒有Plans屬性。解決方案只是在您的模板數據對象中使用正確的屬性名稱:

plansHTML = template({ Plans: context });