如何在 3 秒的超时功能上刷新我的 table

How to refresh my table on a time-out function of 3 seconds

我无法在 5 秒超时时仅刷新 mvc5 应用程序索引页中的 table。我在这里阅读了很多关于它的帖子,但我的理解非常基本。

我有一个名为 Index 的 ActionResult,它 returns 是一个存储过程,用于在我 select 一个值并提交它时填充下拉列表 我有另一个名为 Index 的控制器,但它上面有 [HttpPost]它,这现在保存了下拉列表的 selected 值,将其传递给另一个存储过程,并将 returns 模型传递给视图以创建 table。我想在 5 秒后刷新此 table。我尝试先使用下面的按钮刷新。

<tbody id="refresh"> <!--The Id I use for my table -->
<input id="butt" type="submit" value="Go" /> <!--The button to refresh-->

<script>
    $("#butt").click(function () {
        $.ajax({
            url: '/Home/Index',
            success: function (data) {
                $("#refresh").html(data);
            }
        })
    })
</script>

这是我的控制器:

DBEntities  db = new DBEntites();

public ActionResult Index()
{
    CircuitClass model = new CircuitClass();

    model.MyCircuit = db.GetAllCircuits(DateTime.Today);
    return View(model);
}

[HttpPost]
public ActionResult Index(CircuitClass model)
{
    string circuit = model.SelectedCircuit.ToString();
    int raceNo = model.SelectedRaceNo;
    if (model.SelectedRaceNo == 0 || (model.SelectedCircuit == null))
    {
      return View("~/Views/Shared/Error.cshtml");
    }
    else
        model.MyResults = db.GetCircuitResults(circuit, raceNo, DateTime.Today).ToList();
    model.MyCircuit = db.GetAllCircuits(DateTime.Today);

    return View(model);
}

在调试中,我按下按钮将我带到上面没有 [HttpPost] 的索引控制器,这是我需要刷新 table 的地方。我期待它用 [HttpPost] 加载控制器。 我的方法可能完全错误,有人可以帮忙吗?

使用js中的setInterval函数每5秒调用一次display函数

$("#butt").on('click', display());

function display()
{
    $.ajax({
        url: '/Home/Index',
        success: function (data) {
            $("#refresh").html(data);
        }
    })
}

setInterval(function(){ display(); }, 5000); //Refreshes every 5 sec

(或者)trigger 按钮每 5 秒点击一次

$("#butt").click(function () {
    $.ajax({
        url: '/Home/Index',
        success: function (data) {
            $("#refresh").html(data);
        }
    })
})

setInterval(function(){  $("#butt").trigger('click') }, 5000);