2011-06-02 78 views
0

我有我的小asp.net mvc應用程序按預期工作,但是我錯誤地構建了JSON。在Sencha Touch XTemplate中使用外部JSON

這是我目前的內煎茶

建立一個JSON對象的消費方式
 // note: this is using a RAZOR/C# foreach loop to build a JSON string 
     var videosToShow = [ 
     @foreach (Web.Models.VideoListViewModel video in Model){ 
      @Html.Raw("{ id: " + @video.id + ",") 
      @Html.Raw(" title: \"" + @video.title + "\" },") 
     } 
     ]; 

然後,我有一個煎茶模板

 videoTpl = new Ext.XTemplate([ 
      '<tpl for=".">', 
      '<div>', 
      '<iframe src="http://player.vimeo.com/video/{id}?title=0&amp;byline=0&amp;portrait=0&amp;color=80ceff&amp;fullscreen=1" ', 
      'width="' + screenWidth + '" ', 
      'height="' + screenHeight + '" ', 
      'frameborder="0">', 
      '</iframe>', 
      '&nbsp;&nbsp;{title}', 
      '</div>', 
      '</tpl>' 
     ]); 

以及一個視頻面板

 videoPanel = new Ext.Panel({ 
      title: "Videos", 
      tpl: videoTpl, 
      iconCls: "tv", 
      dockedItems: [{ xtype: "toolbar", title: "Videos"}], 
      scroll: "vertical" 
     }); 

和我的根TabPanel

 rootPanel = new Ext.TabPanel({ 
      fullscreen: true, 
      layout: 'card', 
      region: 'center', 
      items: [videoPanel, biblePanel, aboutPanel, helpPanel, morePanel], 
      tabBar: { dock: 'bottom' } 
     }); 

     videoPanel.update(videosToShow); 

這裏的底線是上述工作正常。我只是不喜歡內部的JSON字符串,我寧願發送來自URL的JSON。

這可能是簡單的,我很想念,但任何影響將不勝感激。

回答

0

做這樣的:

var videosToShow = @Html.Raw(Json.Encode(Model)); 

或當然,如果你的模型有很多特性,你只是想idtitle

var videosToShow = @Html.Raw(Json.Encode(Model.Select(x => new { x.id, x.title }))); 

這將是更好/更容易/短/安全/避免意大利麪代碼/無循環/工作。

編輯 回覆發現在評論中。

+0

但這仍然在我的網頁中刪除JSON,而不是從外部網址消耗。 – 2011-06-02 17:28:19

+0

@rockinthesixstring,你的意思是*從外部網址*消耗? – 2011-06-02 17:29:22

+0

上面的代碼直接在視圖中從模型構建JSON字符串。我需要做的是構建一個服務,從控制器發送JSON並以某種方式使用'$ GetJSON'來填充模板。 – 2011-06-02 17:36:12