清爽的局部视图在MVC 3 [英] Refreshing Partial View in MVC 3

查看:158
本文介绍了清爽的局部视图在MVC 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我已包括在我的_Layout.cshtml的局部视图。它只是一个JavaScript函数,改变基于我的系统状态的图像。我不需要重新加载任何数据,我并不需要去到控制器的任何东西的code,我只需要重新加载局部视图。

I have a partial view that I have included on my _Layout.cshtml. It simply has a javascript function that changes an image based on the state of my system. I don't need to reload any data, I don't need to go to the code of the controller for anything, I simply need to reload that partial view.

我试过很多的,我发现这里的例子,但无法得到其中的任何工作。我觉得,如果他们为我所做的一切反正太复杂。任何指导,将AP preciated。

I tried many of the examples that I found here but couldn't get any of them to work. I felt as if they were too complex for what I was doing anyway. Any guidance would be appreciated.

谢谢,

史蒂夫

推荐答案

如果部分被装入布局直接再有就是刷新它没有简单的方法,因为它基本上完全渲染页面的一部分。

If the partial is loaded into the layout directly then there's no straightforward way to refresh it, because it's basically a part of the complete rendered page.

您最好的选择是通过敲击控制方法和渲染结果放入容器(如DIV呈现局部使用 $。负载或任何等效您有可用)。你将不得不被装载布局本身在脚本内做到这一点,通过观察的document.ready 或类似的东西。一旦你的到位则是微不足道的保持重装,或按控制器方法多次,因为你需要刷新内容。例如jQuery中:

Your best bet is to render the partial using $.load or whatever equivalent you have available by hitting a controller method and rendering the result into a container (like a div). You would have to do this within a script that is loaded with the layout itself, by observing document.ready or something like that. Once you have that in place then it's trivial to keep reloading or refreshing the contents by hitting the controller method as many times as you need. For example in jQuery:

$(document).ready(function () {
    RefreshPartial();
    window.setInterval(RefreshPartial, 10000);
});

function RefreshPartial() {
    $('#container').load('/some/controller/endpoint', {parameters});    
}

这将调用控制器的方法,并设定 #container的标识的元素的内部内容。只要你想,你可以叫 RefreshPartial 多次。

This will call the controller method, and set the inner contents of the element identified with #container. You can call RefreshPartial as many times as you want.

这篇关于清爽的局部视图在MVC 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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