如何使用 MVC Power BI Embedded 传递变量?
How can I pass variables using MVC Power BI Embedded?
我正在使用 visual studio 中嵌入的 Power BI 构建应用程序。我在控制器中的报告操作中添加了用户名和角色,对应于我要嵌入的实际报告中的用户名和角色。我想将用户名作为变量从 index.cshtml 中的文本框传递给 Report 操作;当此值被硬编码时,报告会加载并嵌入,但我无法将其作为变量传递!
这是控制器中的 ActionResult -
public async Task<ActionResult> Report(string reportId)
{
//This is where I'm trying to get the variable from the textbox
username = Request["centreID"].ToString();
using (var client = this.CreatePowerBIClient())
{
var reportsResponse = await client.Reports.GetReportsAsync(this.workspaceCollection, this.workspaceId);
var report = reportsResponse.Value.FirstOrDefault(r => r.Id == reportId);
IEnumerable<string> roles = new List<string>() { "xxxxxxxx" };
// I can hardcode the username here, and it works, but can't pass in a variable from the html like above
//string username = "xxxxxx";
//username = this.username;
var embedToken = PowerBIToken.CreateReportEmbedToken(this.workspaceCollection, this.workspaceId, report.Id, username, roles);
var viewModel = new ReportViewModel
{
Report = report,
AccessToken = embedToken.Generate(this.accessKey)
};
return View(viewModel);
}
}
在模型中我放置了 get 和 sets
public string username { get; set; }
这是我用过的html;我之前在控制器中用 void 方法测试过它,它确实将变量传递给控制器
@using (Html.BeginForm("Report", "Dashboard"))
{
@Html.Label("Enter Centre ID")
@Html.TextBox("centreID")
<input type="submit" value="submit" />
}
</span>
</a>
您的方法需要一个 reportId
,但是当您调用它时,没有使用该名称的控件。现在假设您在 ViewBag
中的上一步中获得了该值,那么您的代码需要按以下方式更新:
@using (Html.BeginForm("Report", "Dashboard"))
{
@Html.Label("Enter Centre ID")
@Html.TextBox("centreID")
<input type="hidden" name="reportId " value="@(ViewBag.reportId ?? "")" />
<input type="submit" value="submit" />
}
还有你的方法,比如:
public async Task<ActionResult> Report(string reportId, string centreID)
{
//This is where I'm trying to get the variable from the textbox
using (var client = this.CreatePowerBIClient())
{
var reportsResponse = await client.Reports.GetReportsAsync(this.workspaceCollection, this.workspaceId);
var report = reportsResponse.Value.FirstOrDefault(r => r.Id == reportId);
IEnumerable<string> roles = new List<string>() { "xxxxxxxx" };
// I can hardcode the username here, and it works, but can't pass in a variable from the html like above
var embedToken = PowerBIToken.CreateReportEmbedToken(this.workspaceCollection, this.workspaceId, report.Id, centreID, roles);
var viewModel = new ReportViewModel
{
Report = report,
AccessToken = embedToken.Generate(this.accessKey)
};
return View(viewModel);
}
}
我正在使用 visual studio 中嵌入的 Power BI 构建应用程序。我在控制器中的报告操作中添加了用户名和角色,对应于我要嵌入的实际报告中的用户名和角色。我想将用户名作为变量从 index.cshtml 中的文本框传递给 Report 操作;当此值被硬编码时,报告会加载并嵌入,但我无法将其作为变量传递!
这是控制器中的 ActionResult -
public async Task<ActionResult> Report(string reportId)
{
//This is where I'm trying to get the variable from the textbox
username = Request["centreID"].ToString();
using (var client = this.CreatePowerBIClient())
{
var reportsResponse = await client.Reports.GetReportsAsync(this.workspaceCollection, this.workspaceId);
var report = reportsResponse.Value.FirstOrDefault(r => r.Id == reportId);
IEnumerable<string> roles = new List<string>() { "xxxxxxxx" };
// I can hardcode the username here, and it works, but can't pass in a variable from the html like above
//string username = "xxxxxx";
//username = this.username;
var embedToken = PowerBIToken.CreateReportEmbedToken(this.workspaceCollection, this.workspaceId, report.Id, username, roles);
var viewModel = new ReportViewModel
{
Report = report,
AccessToken = embedToken.Generate(this.accessKey)
};
return View(viewModel);
}
}
在模型中我放置了 get 和 sets
public string username { get; set; }
这是我用过的html;我之前在控制器中用 void 方法测试过它,它确实将变量传递给控制器
@using (Html.BeginForm("Report", "Dashboard"))
{
@Html.Label("Enter Centre ID")
@Html.TextBox("centreID")
<input type="submit" value="submit" />
}
</span>
</a>
您的方法需要一个 reportId
,但是当您调用它时,没有使用该名称的控件。现在假设您在 ViewBag
中的上一步中获得了该值,那么您的代码需要按以下方式更新:
@using (Html.BeginForm("Report", "Dashboard"))
{
@Html.Label("Enter Centre ID")
@Html.TextBox("centreID")
<input type="hidden" name="reportId " value="@(ViewBag.reportId ?? "")" />
<input type="submit" value="submit" />
}
还有你的方法,比如:
public async Task<ActionResult> Report(string reportId, string centreID)
{
//This is where I'm trying to get the variable from the textbox
using (var client = this.CreatePowerBIClient())
{
var reportsResponse = await client.Reports.GetReportsAsync(this.workspaceCollection, this.workspaceId);
var report = reportsResponse.Value.FirstOrDefault(r => r.Id == reportId);
IEnumerable<string> roles = new List<string>() { "xxxxxxxx" };
// I can hardcode the username here, and it works, but can't pass in a variable from the html like above
var embedToken = PowerBIToken.CreateReportEmbedToken(this.workspaceCollection, this.workspaceId, report.Id, centreID, roles);
var viewModel = new ReportViewModel
{
Report = report,
AccessToken = embedToken.Generate(this.accessKey)
};
return View(viewModel);
}
}