2016-04-13 171 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数据。

但最好的做法是使用强类型视图。

相关问题