如何从 MVC 中的 jQuery 访问 PartialView 中的 ViewData?
我有一个通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于 ASP.NET 在呈现时遍历 HTML 文档的方式。
即使您在
$.get
调用完成后执行此代码:ViewData
属性仍将在渲染时求值(例如在此之前)代码被执行)。我的建议:
在局部视图中声明一个隐藏字段,并将
ViewData
放入其中:渲染局部视图时将对其进行评估 >。
然后你可以通过 jQuery 访问它:
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: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:That will get evaluated when the partial view is rendered.
Then you can access it via jQuery: