如何使用MVC Power BI Embedded传递变量?

时间:2016-08-23 14:29:34

标签: c# asp.net-mvc powerbi powerbi-embedded

我正在使用Visual Studio中嵌入的电源BI构建应用程序。我已将控制台中的用户名和角色添加到控制器中,该操作对应于我要嵌入的实际报表中的用户名和角色。我想将用户名作为变量从index.cshtml中的文本框传递到Report操作;当此值被硬编码时,报告会加载并嵌入,但我无法将其作为变量传递!

这是控制器中的ActionResult -

L.circleMarker

在模型中我放置了我的get和sets

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);
    }
}

这是我用过的html;我之前在控制器中使用void方法对此进行了测试,并将变量传递给控制器​​

public string username { get; set; }

1 个答案:

答案 0 :(得分:0)

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