2013-05-29 77 views
1

我有兩個操作的控制器,並與兩個圖表的圖,第一個是直接在查看和第二個是一個局部視圖,如以下:PartialView開不渲染與ViewBags屏幕值

<div class="chart" style="margin-top: 10px;"> 
     @(Html.Telerik().Chart(Model) 
      .Name("CustomerCount") 
      .Title(title => title.Text("TotalCustomersPerHour")) 
      .Legend(settings => settings.Position(ChartLegendPosition.Bottom)) 
      .Series(series => series.Line(p => p.CustomerCount.Total).Name("TotalCustomersPerHour")) 
      .CategoryAxis(a => a.Categories(p => p.CustomerCount.Intervalo).Labels(l => l.Rotation(-40))).Theme("vista") 
      .Tooltip(true) 
      .HtmlAttributes(new { style = "height: 300px;" }) 
      .DataBinding(dataBinding => dataBinding.Ajax() 
      .Select("CustomerCountData", "Indicators", new 
      { 
       storeId = "TR_001", 
       startDate = DateTime.Today, 
       endDate = DateTime.Today.AddDays(10) 
      })) 
      .ClientEvents(b => b.OnDataBound("OnCustomerCountDataBound")) 
     ) 
</div> 
<div class="chart" style="margin-top: 10px;"> 
     @Html.Partial("_ChartTest") 
</div> 

第一個Action被一個Telerik Chart Ajax調用調用,當它完成後,我通過OnDataBound事件調用另一個Action,以便使用ViewBags填充我的部分視圖圖表。

function OnCustomerCountDataBound(e) { 
    $.ajax({ 
     type: "POST", 
     url: "Indicators/ChartTest", 
     data: { retailStoreId: "TR_001"}, 
    }); 
} 

我調試的局部視圖,我可以得到ViewBag值,但結果不會顯示在頁面上。

@(Html.Telerik().Chart() 
    .Name("TimeOfPermanency") 
    .Title(title => title.Text(TrackerMessages.TimeOfPermanencyPerArea)) 
    .Legend(settings => settings.Position(ChartLegendPosition.Bottom)) 
    .Series(series => 
     { 
      if (ViewBag.Series != null) 
      { 
       foreach (var area in ViewBag.Series) 
       { 
        series.Column(area.Data).Name(area.Name); 
       } 
      } 

     }) 
    .CategoryAxis(axis => 
    { 
     if (ViewBag.Categories != null) 
     { 
      axis.Categories(ViewBag.Categories); 
     } 
    }) 
    .Tooltip(true) 
    .HtmlAttributes(new { style = "height: 300px;" }) 
    .ClientEvents(b => b.OnDataBound("OnTimeOfPermanency")) 
) 

任何人都知道爲什麼結果不是使我的第二個圖表上?

謝謝!

回答

2

我調試了部分視圖,我可以得到ViewBag值,但 結果沒有顯示在頁面上。

這是因爲您似乎沒有在您的ajax調用的成功回調中做任何事情。你甚至沒有訂閱它。

因此,繼續前進,訂閱它,並通過控制器返回的結果更新部分:

function OnCustomerCountDataBound(e) { 
    $.ajax({ 
     type: "POST", 
     url: "Indicators/ChartTest", 
     data: { retailStoreId: "TR_001"}, 
     success: function(result) { 
      $('#childChart').html(result); 
     } 
    }); 
} 

注意,因爲你有2個元素class="chart"和從我的理解,我已經使用了$('#childChart')選擇你只想更新第二個。因此,請確保您分配一個唯一的ID,以包含div:

<div class="chart" id="childChart" style="margin-top: 10px;"> 
    @Html.Partial("_ChartTest") 
</div> 
+0

我沒有,因爲我不上這個AJAX調用返回沒有一個結果,我只是調用填充ViewBags的動作來呈現在我的系列和Telerik Chart的類別部分,如我在上面的代碼中所示。 – felipekm

+1

您使用AJAX調用('Indicators/ChartTest')觸發的控制器操作必須返回_ChartTest局部視圖,並填充ViewBag數據,因爲此部分依賴於該數據。在您的AJAX調用的成功回調中,如果您希望顯示此圖表,您應該刷新DOM的相應部分。 –

+0

男人,像魅力一樣工作。 你只是搖滾!我就知道!謝謝! – felipekm