1
我目前使用嵌入式Power BI將報告嵌入到Web應用程序中,並且我想創建使用下拉框在組中的報告之間切換的功能。我設法使用適當的數據填充下拉框,但現在正努力在報告之間切換。我可以成功地做到這一點,如果我硬編碼到服務器端的電話號碼,但顯然這是不可擴展的,而不是解決方案。下面是我的cshtml我需要一個方法來在reportSelector.onchange中的「@Model」調用能夠看到選定的變量。在服務器端調用中使用javascript變量MVC
@model TaskWebApp.Models.EmbedConfig[]
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@if (!string.IsNullOrEmpty(Model[0].ErrorMessage))
{
<div id="errorWrapper">
<h2>
Error
</h2>
@Model[0].ErrorMessage
</div>
return;
}
<div>
<p>Select report</p>
<select id="reportSelector"></select>
</div>
<div id="reportContainer"></div>
<script>
// Read embed application token from Model
var accessToken = "@Model[0].EmbedToken.Token";
// Read embed URL from Model
var embedUrl = "@Html.Raw(Model[0].EmbedUrl)";
// Read report Id from Model
var embedReportId = "@Model[0].Id";
// Get models. models contains enums that can be used.
var models = window['powerbi-client'].models;
// Embed configuration used to describe the what and how to embed.
// This object is used when calling powerbi.embed.
// This also includes settings and options such as filters.
// You can find more information at https://github.com/Microsoft/PowerBI-JavaScript/wiki/Embed-Configuration-Details.
var config = {
type: 'report',
tokenType: models.TokenType.Embed,
accessToken: accessToken,
embedUrl: embedUrl,
id: embedReportId,
permissions: models.Permissions.All,
settings: {
filterPaneEnabled: true,
navContentPaneEnabled: true
}
};
// Get a reference to the embedded report HTML element
var reportContainer = $('#reportContainer')[0];
// Embed the report and display it within the div container.
var report = powerbi.embed(reportContainer, config);
var reportSelector = $('#reportSelector')[0];
var optionString = "@Html.Action("PopulateDropdown", "Embed", Model[0].Reports)";
reportSelector.onchange = function() {
var selected = this.selectedIndex;
var accessToken = "@Model[selected].EmbedToken.Token";
// Read embed URL from Model
embedUrl = "@Html.Raw(Model[selected].EmbedUrl)";
// Read report Id from Model
embedReportId = "@Model[selected].Id";
// Get models. models contains enums that can be used.
models = window['powerbi-client'].models;
// Embed configuration used to describe the what and how to embed.
// This object is used when calling powerbi.embed.
// This also includes settings and options such as filters.
// You can find more information at https://github.com/Microsoft/PowerBI-JavaScript/wiki/Embed-Configuration-Details.
config = {
type: 'report',
tokenType: models.TokenType.Embed,
accessToken: accessToken,
embedUrl: embedUrl,
id: embedReportId,
permissions: models.Permissions.All,
settings: {
filterPaneEnabled: true,
navContentPaneEnabled: true
}
};
var report = powerbi.embed(reportContainer, config);
}
reportSelector.innerHTML = optionString;
</script>
讓我知道,如果有必要看到模型或控制器。任何幫助表示讚賞,因爲我對MVC有點新,並且無法以我想要的方式使用變量。
編輯:函數創建下拉列表:
public string PopulateDropdown(ODataResponseListReport reports)
{
foreach (Claim claim in ClaimsPrincipal.Current.Claims)
{
if (claim.Type == "extension_GroupID")
{
GroupId = claim.Value;
}
}
string retVal = "";
for (int i = 0; i < reports.Value.Count; i++)
{
retVal += "<option>" + reports.Value.ElementAtOrDefault(i).Name + "</option>";
}
return retVal;
}
什麼下拉列表訪問它? '@ Model'是剃鬚刀代碼 - 它在發送到視圖之前在服務器上解析。代碼如'var accessToken =「@Model [selected] .EmbedToken.Token」;'不起作用,因爲'selected'是一個javascript變量,在那個時候不存在。 –
ive添加了創建控制器中的下拉菜單的功能 –
是的,我發現它的剃刀,但是在正確的方向上推動我如何將這個變量推入控制器方法或沿着這些方向的東西很棒 –