Razor中的"UpdatePanel"(mvc 3)

Tob*_*ias 10 ajax razor asp.net-mvc-3

是否有像Razor的UpdatePanel(在ASPX中)?

我想每30秒自动刷新数据(例如表格,图表......).与每30秒点击以下链接类似:

 @Ajax.ActionLink("Refresh", "RefreshItems", new AjaxOptions() {
     UpdateTargetId = "ItemList",
     HttpMethod = "Post"})
Run Code Online (Sandbox Code Playgroud)

编辑:

我可能应该添加动作链接呈现局部视图.

cshtml中的代码:

<div id="ItemList">
  @Html.Partial("_ItemList", Model)
</div>
Run Code Online (Sandbox Code Playgroud)

控制器中的代码:

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

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

            return RedirectToAction("Index");
        }
    }
Run Code Online (Sandbox Code Playgroud)

如果PartielView可以刷新自己,它将被创建.

Ahm*_*mad 10

您可以尝试使用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>
Run Code Online (Sandbox Code Playgroud)

上面的代码应该放在包含页面中,而不是部分视图页面中.请记住,部分视图不是一个完整的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>
Run Code Online (Sandbox Code Playgroud)

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