我如何通过使用MVC电源BI嵌入式变量

本文关键字:电源 BI 嵌入式 变量 MVC 何通过 | 更新日期: 2023-09-27 18:03:48

我正在构建一个使用power BI嵌入visual studio的应用程序。我已经向控制器中的报告Action添加了一个用户名和角色,该控制器对应于我想要嵌入的实际报告中的用户名和角色。我想传递在用户名作为一个变量从一个文本框在索引。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>

我如何通过使用MVC电源BI嵌入式变量

您的方法期望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);
}
}