javascript - html 不会在部分 View 中呈现

标签 javascript jquery asp.net-mvc d3.js

以下是我用来渲染 html View 的代码。 @{ 布局=空; }

<script type="text/javascript">
    $(document).ready(function () {
        debugger;
        $("#divChart").load('http://localhost/abcd.Portal/BarChart.html');
    });
</script>


<div style="width: 100%;" id="divChart"></div>

我正在尝试加载的 HTML

<style>
    .bar {
        fill: steelblue;
    }

        .bar:hover {
            fill: brown;
        }

    .axis {
        font: 10px sans-serif;
    }

        .axis path,
        .axis line {
            fill: none;
            stroke: #000;
            shape-rendering: crispEdges;
        }

    .x.axis path {
        display: none;
    }
</style>
<body>

    <script>
        debugger;
        var margin = { top: 20, right: 20, bottom: 30, left: 40 },
            width = 960 - margin.left - margin.right,
            height = 500 - margin.top - margin.bottom;

        var x = d3.scale.ordinal()
            .rangeRoundBands([0, width], .1);

        var y = d3.scale.linear()
            .range([height, 0]);

        var xAxis = d3.svg.axis()
            .scale(x)
            .orient("bottom");

        var yAxis = d3.svg.axis()
            .scale(y)
            .orient("left")
            .ticks(10, "%");

        var svg = d3.select("body").append("svg")
            .attr("width", width + margin.left + margin.right)
            .attr("height", height + margin.top + margin.bottom)
          .append("g")
            .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

        $.ajax({
            url: 'http://localhost/abcd.Portal/Dashboard/GetData',
            type: 'GET',
            data: '',
            cache: false,
            dataType: 'text',
            async: true,
            error: function (xhr) {
                //alert('Error: ' + xhr.statusText);
            },
            success: function (result) {
                debugger;
                result = eval(JSON.parse(result));

                x.domain(result.map(function (d) { return d.letter; }));
                y.domain([0, d3.max(result, function (d) { return d.frequency; })]);

                svg.append("g")
                    .attr("class", "x axis")
                    .attr("transform", "translate(0," + height + ")")
                    .call(xAxis);

                svg.append("g")
                    .attr("class", "y axis")
                    .call(yAxis)
                  .append("text")
                    .attr("transform", "rotate(-90)")
                    .attr("y", 6)
                    .attr("dy", ".71em")
                    .style("text-anchor", "end")
                    .text("Frequency");

                svg.selectAll(".bar")
                    .data(result)
                  .enter().append("rect")
                    .attr("class", "bar")
                    .attr("x", function (d) { return x(d.letter); })
                    .attr("width", x.rangeBand())
                    .attr("y", function (d) { return y(d.frequency); })
                    .attr("height", function (d) { return height - y(d.frequency); });
            }
        });

        function type(d) {
            d.frequency = +d.frequency;
            return d;
        }

    </script>
</body>

我没有收到任何错误,但我的 View 未呈现。我正在尝试渲染 d3 条形图。任何我出错的地方的建议。

我有一个加载我的部分 View 的父级。现在,如果我在部分 View 中编写 html 代码,则会出现错误。因此,我尝试加载 html 文件。

最佳答案

这不是使用 jquery 加载部分 View 的方法,请使用操作。

创建一个将返回部分 View 的操作:

public ActionResult BarChart()
{
  return View();
}

在 jquery 中:

$(document).ready(function () {
        debugger;
        $("#divChart").load('@Url.Action("BarChart","ControllerName")');
    });

关于javascript - html 不会在部分 View 中呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29092340/

相关文章:

javascript - 具有动态表单的 jquery 验证插件

javascript - 在 select 选项的 onchange function() 中传递多个名称

jquery - 微帖子字符倒计时(Rails 教程,第二版,第 10 章,练习 7)

javascript - 输入键在 froala 编辑器上不起作用

asp.net-mvc - jQuery.Load() 不触发 ASP.NET MVC2 中的 Request.IsAjaxRequest

asp.net - 我迷路了。 ASP.NET MVC 5发生了什么?

asp.net-mvc - ASP.NET MVC - 不从其他表中获取数据

javascript - onclick显示写在<button>之后的<div>(不是所有的div)

javascript - Chrome iframe 父级未定义

javascript - 为什么是 myPlants[1]? JSON