gpt4 book ai didi

ajax - Razor 中的 "UpdatePanel"(mvc 3)

转载 作者:行者123 更新时间:2023-12-02 09:45:56 24 4
gpt4 key购买 nike

是否有适用于 Razor 的 UpdatePanel(在 ASPX 中)之类的东西?

我想每 30 秒自动刷新一次数据(例如表格、图表……)。类似于每 30 秒点击一次以下链接:

 @Ajax.ActionLink("Refresh", "RefreshItems", new AjaxOptions() {
UpdateTargetId = "ItemList",
HttpMethod = "Post"})

编辑:

我可能应该补充一点,操作链接呈现部分 View 。

cshtml 中的代码:

<div id="ItemList">
@Html.Partial("_ItemList", Model)
</div>

Controller 中的代码:

    [HttpPost]
public ActionResult RefreshItems() {
try {
// Fill List/Model
...

// Return Partial
return PartialView("_ItemList", model);
}
catch (Exception ex) {

return RedirectToAction("Index");
}
}

如果 PartielView 可以自行刷新,则会创建它。

最佳答案

您可以使用 Jquery 尝试类似以下内容(尚未测试)

<script type="text/javascript">
$(document).ready(function() {
setInterval(function()
{
// not sure what the controller name is
$.post('<%= Url.Action("Refresh", "RefreshItems") %>', function(data) {
// Update the ItemList html element
$('#ItemList').html(data);
});
}
, 30000);
});
</script>

上述代码应放置在包含页面中,即不是部分 View 页面中。请记住,部分 View 不是完整的 html 页面。

我最初的猜测是这个脚本可以放在partial中并修改如下。确保 ajax 数据类型设置为 html

<script type="text/javascript">
setInterval(function()
{
// not sure what the controller name is
$.post('<%= Url.Action("Refresh", "RefreshItems") %>', function(data) {
// Update the ItemList html element
$('#ItemList').html(data);
});
}
, 30000);
</script>

另一种替代方法是将 JavaScript 存储在单独的 js 文件中并使用 Jquery getScript ajax成功回调中的函数。

关于ajax - Razor 中的 "UpdatePanel"(mvc 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5866326/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com