更新Div/PartialView上的Interval

Update Div/PartialView on Interval

本文关键字:上的 Interval PartialView Div 更新      更新时间:2023-09-26

我有这个使Ajax调用并返回结果,工作完美。

@foreach (var fighter in Model.Fighters)
{
@Ajax.ActionLink(fighter.FirstName + " " + fighter.LastName, "ShowResults",new            {id        =fighter.FighterID }, new AjaxOptions { HttpMethod = "get", InsertionMode = InsertionMode.Replace, OnSuccess = "SuccessFunction", UpdateTargetId = "results" })
}
<div id="results">
@Html.Partial("Partial1", Model)
</div>
 [HttpGet]
    public ActionResult ShowResults(int id)
    {
        ViewBag.Id = id;
        Fight fight = db.Fights.Find(id);
        if(Request.IsAjaxRequest())
        {
            ViewBag.Message = "The ID is: " + id;
            return PartialView("Partial1", fight);
        }
        return View(fight);
    }

然而,我试图做同样的事情,但部分视图被重新加载在一个设置的间隔。我想这样做:

 $(document).ready(function () {
    $("#round").load("/Fight/ShowResults");
    setInterval(function () {
        $("#results").load("/Fight/ShowResults");
    }, 100000); //Refreshes every 30 seconds
    $.ajaxSetup({ cache: false });  //Turn off caching
});
</script>

但似乎没有做任何事情。什么好主意吗?

我修复了这个问题,我有很多问题需要修复。我应该在问之前仔细看看的。这不会运行的原因是因为我需要在布局页面中创建一个脚本部分,因为我试图运行RenderBody部分的这个脚本。