MVC 3 应用程序中的自动数据加载
好的,我有一个页面,其中使用 jquery/ajax 加载函数从下拉列表动态加载数据。一旦下拉选择发生更改,这允许部分视图动态加载并显示包含所有数据的网格。
这样做的问题是,当页面第一次加载时,数据被加载,这意味着没有 id 选择被发送到 jquery 加载函数。我尝试通过为下拉列表本身添加加载函数来解决此问题,因此当加载它时,它将重新加载数据视图,但无济于事。我认为这是因为控件在数据加载之前加载。但我不知道该怎么办。这是我到目前为止的代码。
<script>
$(document).ready(function () {
$.ajax({
type: 'GET',
url: '@Url.Action("SessionsPartial", "Sessions")',
data: 'id=' + $("#ScheduleTypeId").value,
success: function (data) {
$('#SessionData').fadeOut().html(data).fadeIn();
}
});
$(function () {
$("#ScheduleTypeId").change(function () {
$.ajax({
type: 'GET',
url: '@Url.Action("SessionsPartial", "Sessions")',
data: 'id=' + this.value,
success: function (data) {
$('#SessionData').fadeOut().html(data).fadeIn();
}
});
});
});
$(function() {
$("#ScheduleTypeId").load(function() {
$.ajax({
type: 'GET',
url: '@Url.Action("SessionsPartial", "Sessions")',
data: 'id=' + this.value,
success: function(data) {
$('#SessionData').fadeOut().html(data).fadeIn();
}
});
});
});
});
正如我所说,最后一部分负载部分是我刚刚添加的,因此请随意批评。
Ok, I have a page where the data is loaded dynamically from a dropdown using a jquery/ajax load function. This allows a partial view to load dynamically displaying a grid with all the data once the dropdown selection is altered.
The problem with this is that when the page loads the first time, so data is loaded, which signifies that no id selection is being sent to the jquery load function. I attempted to remedy this by adding a load function for the dropdownlist itself, so when it is loaded, it will reload the data view, but to no avail. I figure it is because the control loads before data is loaded in it. But I am at a loss to what to do. Here is the code I have so far.
<script>
$(document).ready(function () {
$.ajax({
type: 'GET',
url: '@Url.Action("SessionsPartial", "Sessions")',
data: 'id=' + $("#ScheduleTypeId").value,
success: function (data) {
$('#SessionData').fadeOut().html(data).fadeIn();
}
});
$(function () {
$("#ScheduleTypeId").change(function () {
$.ajax({
type: 'GET',
url: '@Url.Action("SessionsPartial", "Sessions")',
data: 'id=' + this.value,
success: function (data) {
$('#SessionData').fadeOut().html(data).fadeIn();
}
});
});
});
$(function() {
$("#ScheduleTypeId").load(function() {
$.ajax({
type: 'GET',
url: '@Url.Action("SessionsPartial", "Sessions")',
data: 'id=' + this.value,
success: function(data) {
$('#SessionData').fadeOut().html(data).fadeIn();
}
});
});
});
});
As I said, the last part with the load part is what I just added, so feel free to critique.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一次加载页面时,您实际上不需要执行 AJAX 调用来加载数据。您可以简单地包含包含 grid: 的部分
,然后仅订阅下拉列表的
.change()
事件来刷新部分:When the page is loaded the first time you don't really need to perform an AJAX call to load the data. You could simply include the partial that contains the grid:
and then only subscribe to the
.change()
event of the dropdown to refresh the partial:问题是 ajax 函数确实调用了将数据加载到局部中的操作函数,但它在初始加载时发送模型的 id,而不是下拉列表的 id,因此数据加载为空。
我通过检查该值是否为 0 然后强制它加载无论如何都会是第一个的项目的 id 来解决这个问题。就像魅力一样。
The problem was that the ajax function was indeed calling the action function that loads the data into the partial, but it was sending the id of the model on the initial load as opposed to the id of the dropdown, so the data was loading empty.
I remedied that by checking if the value was 0 then forcing it to load with the id of the item that will be first at any rate. worked like a charm.