如何从 MVC 中的 jQuery 访问 PartialView 中的 ViewData?

发布于 2024-10-09 16:08:47 字数 1942 浏览 1 评论 0原文

我有一个通过 jQuery 加载 PartialView 的视图。 PartialView 有 jQuery 需要访问的 ViewData,但我似乎无法从 jQuery 访问它。下面的代码使用 HighChart jQuery 插件(它只是从 HighChart 示例页面获取的测试代码):

<script type="text/javascript">
        var chart1; // globally available
        $(document).ready(function () {
            $('#reportButton').click(function (event) {
                var actionUrl = '<%= Url.Action("UserReport") %>';
                var customerId = $('#customersId').val();
                var month = $('#monthsId').val();
                var employees = '<%= ViewData["Employees"] %>'
                alert(employees);
                $.get(actionUrl, { customerId: customerId, month: month }, function (data) {
                    $('#result').html(data);
                    getHighChart();
                });
            });
        });

        function getHighChart() {
            var employees = '<%= ViewData["Employees"] %>'
            alert(employees);
            chart1 = new Highcharts.Chart({
                chart: {
                    renderTo: 'chart-container-1',
                    defaultSeriesType: 'bar'
                },
                title: {
                    text: 'Fruit Consumption'
                },
                xAxis: {
                    categories: [employees]
                },
                yAxis: {
                    title: {
                        text: 'Fruit eaten'
                    }
                },
                series: [{
                    name: 'Jane',
                    data: [1, 0, 4]
                }, {
                    name: 'John',
                    data: [5, 7, 3]
                }]
            });
        }


    </script>

这个 jQuery 位于主视图中,因为我不知道如何将其放入具有文档就绪功能的 PartialView 中。但是,通过将对 HighChart 函数的调用放在 get() 函数的回调中,它可以在分部视图中工作。但访问 ViewData 并不...

我怎样才能实现这一点?我需要从 PartialView 操作方法获取数据才能填充 HighChart 数据...

I have a View loading a PartialView via jQuery. The PartialView has ViewData that the jQuery needs to access, but I can't seem to get to it from the jQuery. The code below uses the HighChart jQuery plugin (and it is just test code taken from the HighChart example page):

<script type="text/javascript">
        var chart1; // globally available
        $(document).ready(function () {
            $('#reportButton').click(function (event) {
                var actionUrl = '<%= Url.Action("UserReport") %>';
                var customerId = $('#customersId').val();
                var month = $('#monthsId').val();
                var employees = '<%= ViewData["Employees"] %>'
                alert(employees);
                $.get(actionUrl, { customerId: customerId, month: month }, function (data) {
                    $('#result').html(data);
                    getHighChart();
                });
            });
        });

        function getHighChart() {
            var employees = '<%= ViewData["Employees"] %>'
            alert(employees);
            chart1 = new Highcharts.Chart({
                chart: {
                    renderTo: 'chart-container-1',
                    defaultSeriesType: 'bar'
                },
                title: {
                    text: 'Fruit Consumption'
                },
                xAxis: {
                    categories: [employees]
                },
                yAxis: {
                    title: {
                        text: 'Fruit eaten'
                    }
                },
                series: [{
                    name: 'Jane',
                    data: [1, 0, 4]
                }, {
                    name: 'John',
                    data: [5, 7, 3]
                }]
            });
        }


    </script>

This jQuery is in the main view, because I don't know how I could put it in the PartialView with the document ready function. But by putting the call to the HighChart function in the callback of the get() function it works in the partial view. But accessing the ViewData doesn't...

How can I achieve this? I need to get the data from the PartialView action method in order to populate the HighChart data...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╭⌒浅淡时光〆 2024-10-16 16:08:47

问题在于 ASP.NET 在呈现时遍历 HTML 文档的方式。

即使您在 $.get 调用完成后执行此代码:

var employees = '<%= ViewData["Employees"] %>'

ViewData 属性仍将在渲染时求值(例如在此之前)代码被执行)。

我的建议:

在局部视图中声明一个隐藏字段,并将 ViewData 放入其中:

<input type="hidden" id ="Employees_ViewData" value="<%: ViewData["Employees"] %>" />

渲染局部视图时将对其进行评估 >。

然后你可以通过 jQuery 访问它:

var employees = $('#Employees_ViewData').val();

The problem is the way ASP.NET traverses the HTML document when rendering.

Even though you have this code executing after the $.get call has finished:

var employees = '<%= ViewData["Employees"] %>'

The ViewData property will get evaluated render-time (e.g before this code is executed).

What i recommend:

Declare a hidden field in your partial view, and place the ViewData in there:

<input type="hidden" id ="Employees_ViewData" value="<%: ViewData["Employees"] %>" />

That will get evaluated when the partial view is rendered.

Then you can access it via jQuery:

var employees = $('#Employees_ViewData').val();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文