简体   繁体   English

如何在不刷新页面MVC C#的情况下显示不同的值

[英]How to display different values without refreshing the page MVC C#

I have a method that is looping through a list of values, what I would like to do is when I open the page to be able to see the values changing without refreshing the current view. 我有一个循环遍历值列表的方法,我想要做的是当我打开页面时能够看到值更改而不刷新当前视图。 I've tried something like the code bellow. 我试过像下面的代码。

public static int myValueReader { get; set; }

public static void ValueGenerator()
{
    foreach (var item in myList)
    {
        myValue = item;
        Thread.Sleep(1000);
    }
}

The actual thing is I want it to read this values even if I close the form. 实际的是,即使我关闭表单,我也希望它读取这些值。 I presume I would need to assign a Task in order to do this, but I was wandering if there is any better way of doing it since it's a MVC application? 我认为我需要分配一个Task才能做到这一点,但是如果有更好的方法,我会徘徊,因为它是一个MVC应用程序?

Here's another way to do it: 这是另一种方法:

  • use AJAX and setTimeout 使用AJAX和setTimeout
  • declare one action in your controller (this one will return your different values) 在控制器中声明一个动作(这个动作将返回不同的值)
  • an integer in your ViewBag , some like: ViewBag.totalItems ViewBag的整数,有些像: ViewBag.totalItems

Declare an action in your controller: This is important, because this will be your connection with your database, or data. 在控制器中声明一个动作:这很重要,因为这将是您与数据库或数据的连接。 This action will receive the itemIndex and will return that item. 此操作将接收itemIndex并将返回该项。 Something like this: 像这样的东西:

[HttpPost]
public JsonResult GetItem(int index) {
    return Json(myList.ElementAt(index));
}

ViewBag.TotalItems : Your view has to know how many Items you have in your list. ViewBag.TotalItems :您的视图必须知道列表中有多少项。 I recommend you to pass that value as an integer via ViewBag : 我建议您通过ViewBag将该值作为整数ViewBag

public ActionResult Index() {
    ViewBag.TotalItems = myList.Count();
    return View();
}

AJAX and setTimeout : Once that you have all of this, you're ready to update your view without refreshing: AJAX和setTimeout :一旦掌握了所有这些,您就可以在不刷新的情况下更新视图了:

<script>
$(function() {
    var totalItems = @Html.Raw(Json.Encode(ViewBag.TotalItems));
    var currentItemIndex = 0;

    var getData = function() {
        $.post("@Url.Action("GetItem")", {index:currentItemIndex}, function(data) {

            // data is myList.ElementAt(index)
            // do something with it

        }).always(function() {
            currentItemIndex++;

            if(currentItemIndex < totalItems) {
                setTimeout(getData, 1000); // get the next item every 1 sec
            }
        })
    }

    getData(); // start updating
})
</script>

Your best bet as @DavidTansey mentioned is to use SignlarR . @DavidTansey提到的最好的选择是使用SignlarR It wraps web sockets and falls back to long polling/etc if the users' browser doesn't support it. 如果用户的浏览器不支持它,它会包装Web套接字并回退到长轮询/等。 Your users will subscribe to specific channels and then you can raise events in those channels. 您的用户将订阅特定频道,然后您可以在这些频道中举办活动。

With regard to your business logic, you'll need to look into async programming techniques. 关于业务逻辑,您需要研究异步编程技术。 Once you start on this, you'll probably have more specific questions. 一旦你开始这个,你可能会有更具体的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 填充中继器而不刷新页面 - c# - Filling a Repeater without Refreshing the page 如何在不刷新ASP.NET C#页面的情况下查看数据库更改 - how to see DB changes without refreshing page in ASP.NET C# 如何从JavaScript代码执行C#方法而不刷新页面? - How to execute a C# method from JavaScript code without refreshing the page? C#和SQL:如何在不刷新DataGridView的情况下更新表? - C# & SQL : how to update table without refreshing DataGridView? 如何在不刷新 C# 中 window 的图像的情况下刷新 window? - how to refresh a window without refreshing the images of the window in C#? 在会话尚未结束时停止页面刷新 ASP.NET MVC C# - Stop the page refreshing when the session has not ended ASP.NET MVC C# C#MVC4 Razor部分视图 - 切换视图时整页刷新? - C# MVC4 Razor Partial Views - Full page refreshing while switching views? 分页浏览数据集而无需在MVC C#中获取新数据集 - Page through dataset without getting new dataset in MVC c# 如何在C#中没有窗口的情况下显示图像 - how to display an image without a window in C# 如何在不刷新MVC 3的子弹出窗口中刷新父页面的情况下刷新父JQ网格? - How to refresh parent JQ grid without refreshing the parent page from child popup window in MVC 3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM