在间隔上更新Div / Partial View

时间:2013-03-28 21:20:22

标签: javascript jquery asp.net ajax asp.net-mvc-4

我有这个进行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>

但似乎没有做任何事情。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我解决了这个问题,我遇到了一些我需要解决的问题。在问之前我应该​​仔细看一下。这不会运行的原因是因为当我尝试运行RenderBody部分的脚本时,我需要在布局页面中创建一个脚本部分。