1

Can anyone tell me why this pics of code is not refreshing the view in every 1 sec

 <div id="AutoRefresh">
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Employees.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.RequestTypes.RequestTypeName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Comments)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Inserted_Date)
        </th>

        <th>Action</th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Employees.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.RequestTypes.RequestTypeName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Comments)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Inserted_Date)
            </td>

            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
                @Html.ActionLink("Details", "Details", new { id = item.Id }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.Id })
            </td>
        </tr>
    }

</table>

@section Scripts {
<script type="text/javascript">
    $(function () {
        setInterval(function () { $('#AutoRefresh').load('/RequestApprovals/Index'); }, 1000); // every 1 sec
    });
</script>

}

Controller Action Method:

 // GET: RequestApprovals
    public async Task<ActionResult> Index()
    {
        var requestApprovals = db.RequestApprovals.Include(r => r.Employees).Include(r => r.RequestTypes);
        return View(await requestApprovals.ToListAsync());
    }

I have a _Layout.cshtml file in my shared folder if i use meta tag inside layout page it refresh all the view that use that _Layout.cshtml but i want only one view that contains above code should refresh. I tried above code but it is not working for me. please suggest me what i should do ?

1 Answers1

-1

You can use Ajax call to refresh the page data:

<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script id="sessionJs" type="text/javascript">

   function GetData()
       {
              $.ajax(
              {
                   type: "POST",
                   url: "RequestApprovals/Index",
                   data: "{'value':'" + value + "'}",
                   contentType: "application/json; charset=utf-8", 
                   dataType: "json",
                   failure: function (msg) { alert(msg); },
                   success: function (msg) { alert(msg.d);  } 
                    }
                );
            }
       }
    setTimeout(GetData, 1000);
</script>

Or you can also try using :

initialize: function() {
    this.timer = setInterval(function() {
    this.collection.fetch();
   }, 1000);
},
close: function() {
    clearInterval(this.timer);
}
VikrantMore
  • 903
  • 7
  • 25