简体   繁体   中英

How can I pass variables using MVC Power BI Embedded?

I am building an app using power BI embedded in visual studio. I have added a username and roles to the report Action in the controller that corresponds to a username and role in the actual report I want to embed. I want to pass in the username as a variable from a textbox in the index.cshtml to the Report action; the report loads and embeds when this value is hardcoded but I can't pass it in as a variable!

Here is the ActionResult in the controller -

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

In the model is where I've put my get and sets

public string username { get; set; }

Here is the html I've used; I've tested this with a void method in the controller before and it does pass the variable into the controller

@using (Html.BeginForm("Report", "Dashboard"))
{
    @Html.Label("Enter Centre ID")
    @Html.TextBox("centreID")

    <input type="submit" value="submit" />
}
</span>
</a>

You method is expecting a reportId , but when you are calling it, there is no control with that name. Now let's suppose that you have that value from a previous step inside a ViewBag , then you code need to be updated in the following way:

@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" />
}

And your method, like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM