2016-05-17 66 views
1

我正在爲我的MVC控制器中的Jquery flot生成數據,並且希望使用更復雜的結構,以便能夠更改每個條的顏色等。如何將JSON數據從MVC4傳遞到Flot而無需編寫適配器

要做到這一點我需要傳遞data structure that looks following

{ 
label: "y = 3", 
data: [[0, 3], [10, 3]] 
} 

我傳遞數據時,它是簡單的數組通過簡單地獲取數據和塑造它像下面

var data = topAgents.Select(agent => new List<object> 
     { 
      agent.User != null ? string.Format("{0}", agent.User).Replace("SAGANTGB\\", "") : null, 
      agent.Amount 
     }).ToList(); 

     return Json(data, JsonRequestBehavior.AllowGet); 
沒問題

,並在JavaScript方面:

function onAgentDataReceived(series) { 
     $("#agents").empty(); 
     $.plot("#agents", [series], { bars: { show: true, barWidth: 0.6, align: "center" }, xaxis: { mode: "categories", tickLength: 0 } }); 
    } 

但是現在我似乎無法能夠將數據塑造成這樣,將適合什麼$ .plot期待的結構。

到目前爲止,我曾嘗試:

的鍵值對:

var grouped = result.GroupBy(o => o.CREATE_USER_ID).Select(agent => new 
       { 
        data = new KeyValuePair<string,int>(
        agent.Key != null ? string.Format("{0}", agent.Key).Replace("SAGANTGB\\", "") : null, agent.Count() 
         ), 
        color = "yellow" 
       }).ToList(); 

對象與屬性:

var grouped = result.GroupBy(o => o.CREATE_USER_ID).Select(agent => new 
       { 
        data = new { A= 
        agent.Key != null ? string.Format("{0}", agent.Key).Replace("SAGANTGB\\", "") : null, B = agent.Count() 
         }, 
        color = "yellow" 
       }).ToList(); 

名稱值集合

var grouped = result.GroupBy(o => o.CREATE_USER_ID).Select(agent => new 
       { 
        data = new NameValueCollection 
        {{ 
         agent.Key != null ? string.Format("{0}", agent.Key).Replace("SAGANTGB\\", "") : null, agent.Count().ToString()} 
        }, 
        color = "yellow" 
       }).ToList(); 

匿名對象數組

var grouped = result.GroupBy(o => o.CREATE_USER_ID).Select(agent => new 
       { 
        data = new object[] { agent.Key != null ? string.Format("{0}", agent.Key).Replace("SAGANTGB\\", "") : null, agent.Count()}, 
        color = "yellow" 
       }).ToList(); 

但他們沒有飛過

恕我直言簡單data = new object { agent.Key, agent.Count()}, 應該工作,但它在構建失敗:Error 16 Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

我可以在重新獲取JS後的數據,但想避免這個不必要的步驟。

如何將數據從MVC傳遞到JavaScript而不必在JS端重新塑造它?

回答

0

寫這個問題20分鐘後,成功地在未來嘗試

var grouped = 
    result.GroupBy(o => o.CREATE_USER_ID).Select(agent => new 
    { 
     data = new List<List<object>> 
     { 
      new List<object> 
      { 
       agent.Key != null ? string.Format("{0}", agent.Key).Replace("SAGANTGB\\", "") : null, 
       agent.Count() 
      } 
     }, 
     color = "yellow" 
    }).ToList(); 

return Json(grouped, JsonRequestBehavior.AllowGet); 

顯然,你需要加倍包裝在列表中。

忘了提就不再需要包裝在陣列上JS側

$.ajax({ 
     url: '/dashboard/Home/CancellationAgents/', 
     type: "GET", 
     dataType: "json", 
     success: onCancellationAgentsReceived 
    }); 

    function onCancellationAgentsReceived(series) { 
     $("#agentcancellations").empty(); 
     $.plot("#agentcancellations", series, { 
      bars: { show: true, barWidth: 0.7, align: "center" }, xaxis: {mode: "categories", tickLength: 0, position: "bottom"}}); 
    } 

希望這可以節省你一些時間

相關問題