我是 MVC 的新手,我使用此链接作为教程,将来自 canvasjs 的图形合并到模态中。 https://canvasjs.com/docs/charts/integration/asp-net-mvc-charts/
按照此处的指示进行操作(仅静态显示),它会在窗口加载时完美加载,正如基于此代码所预期的那样
<script type="text/javascript">
window.onload = function () {
var chart = new CanvasJS.Chart("chartContainer", {
theme: "theme2",
animationEnabled: true,
title: {
text: "Simple Column Chart in ASP.NET MVC"
},
subtitles: [
{ text: "Try Resizing the Browser" }
],
data: [
{
type: "column", //change type to bar, line, area, pie, etc
dataPoints: [
{ x: 10, y: 71 },
{ x: 20, y: 55 },
{ x: 30, y: 50 },
{ x: 40, y: 65 },
{ x: 50, y: 95 },
{ x: 60, y: 68 },
{ x: 70, y: 28 },
{ x: 80, y: 34 },
{ x: 90, y: 14 }
]
}
]
});
chart.render();
};
</script>
我现在想要的是遵循相同的简单指令并在模态内显示图形。
我有一张表,其中一列中有“查看图表”按钮”,此过程设置为根据数据库中的记录动态显示图表。但现在,我只想显示图表教程链接。
这个按钮正在调用一个 javascript 函数并在 Controller 中调用一个 Action ,在 Controller 内部,它将将从 View 模型获取的数据传递给局部 View ..
这是函数的javascript
patient.js
// Modal view user details
function ViewVVSDetails(patientId, type, graph) {
var options = { "backdrop": "static", keyboard: true };
$.ajax({
type: "GET",
data: { patientId: patientId, type: type, graph: graph },
url: "/Patient/GetVisitVitalSignDetails",
contentType: "application/json; charset=utf-8",
datatype: "json",
success: function (data) {
console.log(data);
$('#modalVVSDetailsContent').html(data);
$('#modalVVSDetails').modal(options);
$('#modalVVSDetails').modal('show');
},
error: function () {
bootbox.alert({
size: "small",
title: "Error!",
message: "There is an error while loading data."
});
}
});
}
这里是view,modal的div放在哪里,table和button所在的主页,只会显示div modal的代码
patientInfo.cshtml
<!-- Modal: Clinic Details -->
<div id="modalVVSDetails" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div id="modalVVSDetailsContent" class="modal-content"></div>
</div>
</div>
这是 Controller
patientController.cs
[HttpGet]
public IActionResult GetVisitVitalSignDetails(int patientId, string type, string graph)
{
var visitSign = _patient.GetVisitVitalSignHeight(patientId);
if (type == "h")
{
if (graph == "hgn")
{
return PartialView("_ViewVisitVitalSignDetails", visitSign);
}
else
{
return PartialView("_ViewVisitVitalSignGraphDetails", visitSign);
}
}
else
{
if (graph == "wgn")
{
return PartialView("_ViewVisitVitalSignWeightDetails", visitSign);
}
else
{
return PartialView("_ViewVisitVitalSignWeightGraphDetails", visitSign);
}
}
}
这是名为 (_ViewVisitVitalSignGraphDetails) 的部分 View ,其中显示图形的代码是其中的一部分。 我刚刚从原始代码中删除了 window.onload,因为现在不是这个想法。
_partielPatientModal.cshtml
@using UMP.ClinicalSystem.Models.Models;
@using UMP.ClinicalSystem.Models.Dto;
@model IEnumerable<VisitVitalSignVM>
@{
Layout = null;
}
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
<div id="chartContainer"></div>
<script type="text/javascript">
$( function () {
var chart = new CanvasJS.Chart("chartContainer", {
theme: "theme2",
animationEnabled: true,
title: {
text: "Simple Column Chart in ASP.NET MVC"
},
subtitles: [
{ text: "Try Resizing the Browser" }
],
data: [
{
type: "column", //change type to bar, line, area, pie, etc
dataPoints: [
{ x: 10, y: 71 },
{ x: 20, y: 55 },
{ x: 30, y: 50 },
{ x: 40, y: 65 },
{ x: 50, y: 95 },
{ x: 60, y: 68 },
{ x: 70, y: 28 },
{ x: 80, y: 34 },
{ x: 90, y: 14 }
]
}
]
});
chart.render();
});
</script>
我试着在里面输入文本进行测试,它已经显示了模态,但不是图表。因此,链接中的示例 javascript 图在放入模态中时不起作用,并且在我删除 window.onload 之后。
<div id="chartContainer">sample text</div>
主要问题,如何在调用的模态中显示图形
附加信息
这是去到action结果后的结果
这里已经是显示模式的屏幕截图,但不是图形
最佳答案
Controller 返回 html View ,但您的 ajax 请求内容类型是 json。从 ajax 请求中删除这一行。
contentType: "application/json; charset=utf-8",
关于c# - 在 asp.net mvc 中显示模式内的图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45237780/