【发布时间】:2017-07-24 10:07:48
【问题描述】:
我是 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函数并调用控制器中的一个动作,在控制器内部,它将把从viewmodel获取的数据传递给一个partialview..
这是函数的 javascript
耐心.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."
});
}
});
}
这是视图,其中放置了模态的 div,以及位于表格和按钮的主页,将仅显示 div 模态的代码
患者信息.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>
这是控制器
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) 的局部视图,其中显示图形的代码是其中的一部分。 我刚刚从原始代码中删除了 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>
主要问题,如何在被调用的模式中显示图表
附加信息
这是进入动作结果后的结果
这是显示模式的屏幕截图,但不是图表
【问题讨论】:
-
使用相同的代码,我能够正确呈现图表。可能该元素可能被隐藏。还要在 Ajax 成功处理程序中添加
debugger;语句,并检查是否返回了部分视图中的预期 html。如果控件达到在您的控制器逻辑内返回_ViewVisitVitalSignGraphDetails,还要检查一个断点。 -
你好,我在里面放了调试器,实际上是成功的结果,数据实际上是部分视图的内容,即调用图形的javascript代码,但结果到黑色背景
-
我已经更新了问题,显示了图像中实际进入成功块的位置
-
所以我的问题是,您是否尝试将响应 html 直接保留在页面上的 div 中?如果它正确呈现,那么它是您的模态显示的问题,而不是组件或其呈现的问题。还要更新使用什么组件来显示模式?
-
嗨@SivaGopal 我已经更新了我的问题,现在包括用于显示模态的视图中的 div。非常感谢您对此的专家建议
标签: c# asp.net asp.net-mvc bootstrap-modal