当我使用
ViewData
将数据库信息从控制器传输到视图时,与波斯语图表标签相关的信息无法正确显示。事实上,当我用 <text>
标签将信息从 C# 列表传输到 js 数组时 信息可以以 ASCII
格式传输。请帮助我
C#
控制器代码:
public IActionResult DisplayChartsProvince()
{
if ((User.Claims.FirstOrDefault(c => c.Type == "role").Value == "مدیریت") || (User.Claims.FirstOrDefault(c => c.Type == "role").Value == "معاونت فناوری اطلاعات"))
{
//نمودار دایره ای
var data = _context.ProvinceLevel.ToList();
var roleTitles = _context.Roles.Select(r => r.RoleTitle).ToList();
List<int> role = new();
List<string> roleLabels = new();
for(int i = 0; i < roleTitles.Count; i++)
{
int count = data.Where(r => r.role == roleTitles[i]).ToList().Count;
if(data.Where(r => r.role == roleTitles[i]).ToList().Count > 0)
{
roleLabels.Add(roleTitles[i]);
}
if (count!=0)
{
role.Add(count);
}
count = 0;
}
ViewData["roleTitles"] = roleLabels;
ViewData["roleCounts"] = role;
//////////////////////////////////////////////////////
return View();
}
return View("NotAccessProvince");
}
HTML
代码:
@{
ViewData["Title"] = "گزارش استان - سطح استان";
List<string> RoleTitles = ViewData["roleTitles"] as List<string>;
List<int> RoleCounts = ViewData["roleCounts"] as List<int>;
}
<script src="~/js/chartjs/chart.js"></script>
<div class="btn btn-outline-primary btn-sm my-1 mx-3">گزارش سطح استان</div>
<div class="w-100 d-flex my-2 px-3">
<div class="card w-100 d-block">
<div class="card-header text-primary">
<i class="fa fa-pie-chart" style="background-color:"></i>
</div>
<div class="card-body">
<canvas id="d1" class="chart-canvas mx-auto"></canvas>
</div>
</div>
</div>
@section ChartScripts{
<script>
var chartDataOne = [];
var chartLabelsOne = [];
@foreach(var item in RoleCounts)
{
<text>chartDataOne.push(@item);</text>
}
@foreach(var item in RoleTitles)
{
<text>chartLabelsOne.push("@item");</text>
}
var colors = ["blue", "yellow", "green", "orange"];
var ctx = document.getElementById("d1").getContext("2d");
new Chart(ctx, {
type: "pie",
data: {
labels: chartLabelsOne,
datasets: [{
backgroundColor: colors,
data: chartDataOne
}]
},
options: {
title: {
display: true,
text: "World Wide Wine Production 2018"
}
}
});
let valuesArray = chartLabelsOne.values();
for (let item of valuesArray) {
console.log(item);
};
</script>
}
ViewData
中且必须显示的标签:
页面显示:
存储在
chartLabelsOne
数组中的值转换为 ASCII: