2016-04-13 170 views
0

我想從我的視圖中的控制器中使用方法(在我的情況下爲光標)返回的結果, 以便從數據返回。 我寫了一段代碼,但我還沒有找到如何將數據傳遞給視圖。如何將控制器返回的結果從控制器傳遞到視圖

//controller 
    [HttpPost] 
    public async System.Threading.Tasks.Task<ActionResult> drawgraph(Inputmodel m) 
    { 
     List<Client> client = new List<Client>(); 
     var collection = db.GetCollection<Client>("Client"); 
     var builder = Builders<Client>.Filter; 
     var beginDate = Convert.ToDateTime(m.date_begin).Date; 
     var endDate = Convert.ToDateTime(m.date_end).Date; 
     var filter = builder.Gte("date", beginDate) & builder.Lt("date", endDate.AddDays(1)) & builder.Eq("field2", m.taux); 
     var cursor = await collection.DistinctAsync<double>("field2",filter);  
     return View(cursor); 


    } 

//view 
@{ 
    var myChart = new Chart(width:600,height: 400) 
       .AddTitle("graphique") 
       .AddSeries(chartType: "Line") 
       .DataBindTable(dataSource: cursor, xField: "date", yField:"field2") //here I want to use the returnet result by drawgraph in the controller 
       .Write(); 
    } 
+0

您需要創建一個模型,將數據傳遞給它,然後在視圖中使用該模型。 – Kami

回答

0

必須大力鍵入您的視圖訪問模式:

@model YourModelTypeHere @*type of the cursor variable*@ 
//view 
@{ 
    var myChart = new Chart(width:600,height: 400) 
         .AddTitle("graphique") 
         .AddSeries(chartType: "Line") 
         .DataBindTable(dataSource: Model, xField: "date", yField:"field2") //here I want to use the returnet result by drawgraph in the controller 
         .Write(); 
} 

然後你就可以使用Web視圖頁的模式屬性來獲取你的模型。

請參閱article

0

你可以在控制器

ViewBag.MyData = cursor; 

//在視圖中使用

@ViewBag.MyData 

從控制器acceess數據。

但最好的做法是使用強類型視圖。

相關問題