ajax – “UpdatePanel”在Razor(mvc 3)
发布时间:2020-12-16 01:35:19 所属栏目:百科 来源:网络整理
导读:是否有类似UpdatePanel(在ASPX中)为Razor? 我想每30秒自动刷新数据(例如表格,图表,…). 类似于每30秒点击一次链接: @Ajax.ActionLink("Refresh","RefreshItems",new AjaxOptions() { UpdateTargetId = "ItemList",HttpMethod = "Post"}) 感谢Tobi 编辑:
是否有类似UpdatePanel(在ASPX中)为Razor?
我想每30秒自动刷新数据(例如表格,图表,…). @Ajax.ActionLink("Refresh","RefreshItems",new AjaxOptions() { UpdateTargetId = "ItemList",HttpMethod = "Post"}) 感谢Tobi 编辑: 我可能应该补充说,动作链接呈现局部视图. cshtml中的代码: <div id="ItemList"> @Html.Partial("_ItemList",Model) </div> 控制器中的代码: [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> 上述代码应该放在包含的页面中,而不是局部视图页面.请记住,部分视图不是一个完整的HTML页面. 我最初的猜测是,这个脚本可以放在部分和修改如下.确保ajax数据类型设置为html. <script type="text/javascript"> setInterval(function() { // not sure what the controller name is $.post('<%= Url.Action("Refresh",function(data) { // Update the ItemList html element $('#ItemList').html(data); }); },30000); </script> 另一个选择是将javascript存储在单独的js文件中,并在ajax成功回调中使用Jquery getScript函数. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |