刷新时仅重新加载HTML页面中的div [英] Reload only div in HTML page when refresh
问题描述
我使用razor .NET在局部视图中有一个html页面。目前我每隔5分钟刷一次整个部分视图页面,工作正常。我计划通过重新加载html页面内的div而不刷新整个html /部分视图来增强它。
是否有任何我可以参考的文章或链接?我知道JavaScript有一个.load()但它似乎不是我想要的(或者我错过了什么?)
什么我试过了:
在网上寻找解决方案,但没有一个能满足我的要求。
< blockquote>主视图
< script type = text / javascript >
( document )。ready( function (){
window .setInterval(updatePartial, 1000 );
});
function updatePartial(){
// 第二个参数是一个空对象{},这是你传递的地方
// 你的param数据,但如果你离开param,则使用GET并且到期
// 为了追逐你不会看到页面更改,所以我传递了一个空对象
// < span class =code-comment>强制POST
( #p)。load(' @ Url.Action(ShowPartial,首页)',{});
}
< / 脚本 >
< div id = m > 主视图: @ DateTime 。现在< / div >
< div id = p > @ Html .Partial( _ myPartialView)< / div >
< / body >
< / html >
局部视图
部分视图: @ DateTime 。现在
控制器
public ActionResult ShowPartial()
{
return PartialView( < span class =code-string> _ myPartialView);
}
以下是将数据传递到局部视图的示例
< script type = text / javascript >
Hi, I have a html page in partial view using razor .NET. Currently I am doing whole partial view page refreshing every 5 minutes and it works fine. I plan to enhance it by reload only the div inside the html page without refreshing the whole html/partial view.
Is there any article or link that I can refer to? I know JavaScript has a .load() but it does not seems to be what I want(or have I missed out something on it?)
What I have tried:
Looked online for solution but none really hit my requirement.
解决方案 main view
<script type="text/javascript">
(document).ready(function () {
window.setInterval(updatePartial, 1000);
});
function updatePartial() {
// the second param is an empty object {}, this is where you would pass
// your param data, but if you leave the param off a GET is used and due
// to chaching you won't see the page change, so I passed an empty object
// to force a POST
("#p").load('@Url.Action("ShowPartial", "Home")', {});
}
</script>
<div id="m">Main view: @DateTime.Now</div>
<div id="p">@Html.Partial("_myPartialView")</div>
</body>
</html>
partial view
Partial view: @DateTime.Now
controller
public ActionResult ShowPartial()
{
return PartialView("_myPartialView");
}
Here is an example for passing data to the partial view
<script type="text/javascript">
这篇关于刷新时仅重新加载HTML页面中的div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!