"的UpdatePanel"在剃刀(MVC 3) [英] "UpdatePanel" in Razor (mvc 3)

查看:130
本文介绍了"的UpdatePanel"在剃刀(MVC 3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么样的UpdatePanel(在ASPX)的剃刀?

Is there something like UpdatePanel (in ASPX) for Razor?

我要刷新的数据(如表,图表,...),全自动每30秒。 类似点击下面的链接每30秒:

I want to refresh data (e.g. table, chart, ...) automaticly every 30 seconds. Similar to clicking the following link every 30 seconds:

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

由于托比

编辑:

我可能要补充一点,操作链接呈现的局部视图。

I may should add that the action link renders a partial view.

code在CSHTML:

Code in cshtml:

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

code。在控制器:

Code in Controller:

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

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

            return RedirectToAction("Index");
        }
    }

这将是创造,如果PartielView能刷新自己。

It would be create if the PartielView could refresh itself.

推荐答案

您可以尝试类似于以下使用jQuery(未虽然测试)

You can try something similar to the following using Jquery (have not tested though)

<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>

以上code应该放在包含页即不是局部视图页面。请记住,在一个局部视图不是一个完整的HTML页面。

The above code should be placed in the containing page i.e. not the partial view page. Bear in mind that the a partial view is not a complete html page.

我最初的猜测是,该脚本可以放置在部分和修改如下。确保AJAX的数据类型设置为 HTML

My initial guess is that this script can be placed in the partial and modified as follows. Make sure that the ajax data type is set to 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加入功能阿贾克斯成功的回调。

Another alternative is to store the javascript in a separate js file and use the Jquery getScript function in ajax success callback.

这篇关于&QUOT;的UpdatePanel&QUOT;在剃刀(MVC 3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆