ajax Razor (mvc 3) 中的“更新面板”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5866326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 11:00:02  来源:igfitidea点击:

"UpdatePanel" in Razor (mvc 3)

ajaxasp.net-mvc-3razor

提问by Tobias

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

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

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

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

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

Edit:

编辑:

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

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

Code in cshtml:

cshtml中的代码:

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

Code in Controller:

控制器中的代码:

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

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

            return RedirectToAction("Index");
        }
    }

It would be create if the PartielView could refresh itself.

如果 PartielView 可以自我刷新,它将被创建。

采纳答案by Ahmad

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

您可以使用 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>

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.

上面的代码应该放在包含页面中,即不是部分视图页面。请记住,部分视图不是完整的 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.

我最初的猜测是,这个脚本可以放在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>

Another alternative is to store the javascript in a separate jsfile and use the Jquery getScriptfunction in ajax success callback.

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

回答by Tobias

If someone wants the complete code for a selfupdating partial view have a look!

如果有人想要自更新局部视图的完整代码,请查看!

Code of the Controller:

控制器代码:

[HttpPost]
public ActionResult RefreshSelfUpdatingPartial() {

            // Setting the Models Content
            // ...

            return PartialView("_SelfUpdatingPartial", model);
}

Code of the Partial (_SelfUpdatingPartial.cshtml):

部分代码(_SelfUpdatingPartial.cshtml):

@model YourModelClass

<script type="text/javascript">
setInterval(function () {
    $.post('@Url.Action("RefreshSelfUpdatingPartial")', function (data) {
            $('#SelfUpdatingPartialDiv').html(data);
        }
    );
}, 20000);
</script>
// Div
<div id="SelfUpdatingPartialDiv">

// Link to Refresh per Click
<p>
@Ajax.ActionLink("Aktualisieren", "RefreshFlatschels", new AjaxOptions() {
UpdateTargetId = "FlatschelList",
HttpMethod = "Post", InsertionMode = InsertionMode.Replace
})
</p>

// Your Code
// ...

</div>

Code to integrate the Partial in the "Main"-View (ViewWithSelfupdatingPartial.cs):

将 Partial 集成到“主”视图 (ViewWithSelfupdatingPartial.cs) 中的代码:

 @Html.Partial("_FlatschelOverview", Model)

回答by Julian S

Well, if you don't need the AJAX expierience than use the HTML tag:

好吧,如果您不需要 AJAX 体验,那么使用 HTML 标签:

<meta http-equiv=”refresh” content=”30; URL=http://www.programmingfacts.com”>

go here: http://www.programmingfacts.com/auto-refresh-page-after-few-seconds-using-javascript/

去这里:http: //www.programmingfacts.com/auto-refresh-page-after-few-seconds-using-javascript/

回答by Balaji Birajdar

The <meta refresh ..>tag in HTML will work for you. Its the best option

<meta refresh ..>HTML 中的标签对你有用。它是最好的选择

回答by Jonathan

Traditional controls don't works in ASP MVC

传统控件在 ASP MVC 中不起作用

You could do it using Jquery timers http://plugins.jquery.com/project/timers

您可以使用 Jquery 计时器http://plugins.jquery.com/project/timers

Other option could be to use the Delayfunction

其他选项可能是使用延迟功能

In your target is as simple as refresh the whole page, this SO link will be of your interest: Auto refresh in ASP.NET MVC

在您的目标中就像刷新整个页面一样简单,此 SO 链接将引起您的兴趣:Auto refresh in ASP.NET MVC

Hope It Helps.

希望能帮助到你。