問題描述
如何將字節數組作為參數發送到 HTML.Action? (How to send byte array as paramater to HTML.Action?)
我今天一直在使用圖表並且我認為我終於找到了一種方法來讓這一切正常工作,但我遇到了一個我不知道如何通過的問題。
在我的控制器中創建我的圖表:
foreach (var m in model[0].HistoryValues)
{
var chart = new Chart(width: 300, height: 200)
.AddSeries(
chartType: "bar",
xValue: new[] { "Server", "Db", "Tickets" },
yValues: new[] { m.ServerPerformance, m.Databaseperformance, m.SoldTicketsLastUpdate })
.GetBytes("png");
m.Bytes = chart;
//m.ChartFile = File(chart, "image/bytes");
};
現在我想在視圖中將它們顯示為圖像:
@foreach (var m in Model[0].HistoryValues)
{
<img src="@Html.Action("getImage", "OverWatch", new { byte[] Mybytes= m.Bytes })" alt="Person Image" />
}
但我得到了:
無效的匿名類型成員聲明。匿名類型成員必須使用成員分配、簡單名稱或成員訪問來聲明。
getImage 方法:
public FileContentResult getImage(byte[] bytes)
{
return new FileContentResult(bytes, "image/jpeg");
}
我該如何解決這個問題?
參考解法
方法 1:
In an anonymous type you dont define the variable type byte[]. It works it out itself based on the type of m.Bytes
@foreach (var m in Model[0].HistoryValues)
{
<img src="@Html.Action("getImage", "OverWatch", new { Mybytes= m.Bytes })" alt="Person Image" />
}