2

I am working with jQuery Ajax. I want to load data of my table every 5 seconds. Here's what I tried but it's not working. It doesn't return any error or result.

public ActionResult Index()
{
  return View();
}

public PartialViewResult _List()
{
  List<Purchase> model = db.Purchases.ToList();
  return PartialView("_List", model);
}
<div id="loadList"></div>

@section scripts{
  <script>
    $(document).ready(function () {
      setInterval(function () {
        $("#loadList").load("~/Views/Purchases/_List.cshtml");
      }, 3000);
    });
  </script>
}

The Partial View which I want to load in #loadList div.

@model IEnumerable<ChocolateFactory.Data.Purchase>

<table class="table">
<tr>
    <th>@Html.DisplayNameFor(model => model.RefNo)</th>
    <th>@Html.DisplayNameFor(model => model.Date)</th>
    <th>@Html.DisplayNameFor(model => model.Amount)</th>
</tr>
@foreach (var item in Model) {
<tr>
    <td>@Html.DisplayFor(modelItem => item.RefNo)</td>
    <td>@Html.DisplayFor(modelItem => item.Date)</td>
    <td>@Html.DisplayFor(modelItem => item.Amount)</td>
</tr>
}
</table>

The partial View is in:

~/Views/Purchases/_List.cshtml
4
  • You have to pass the url of the controller method that returns the view, not the .cshtml file name in the .load() function - e.g. .load("@Url.Action("_List", YourController"); Commented Nov 1, 2018 at 9:07
  • @StephenMuecke I also tried this. but it's not working. The #loadList is not loading in Index view. Commented Nov 2, 2018 at 9:52
  • $("#loadList").load("@Url.Action("_List", YourController") works just fine. What errors are you getting in the browser console? And we do not know what your _List.cshtml view is Commented Nov 2, 2018 at 9:56
  • I updated question. and it doesn't return any error. Sorry for my English bro. Commented Nov 2, 2018 at 10:58

1 Answer 1

1

The issue is because the tilde character is a Razor construct for the root of the site. It's not recognised outside of that, hence it won't work in your JS code. As you've placed this logic within a view, you can use @Url.Content() to parse the URL before outputting it to the JS:

setInterval(function () {
  $("#loadList").load("@Url.Content("~/Views/Purchases/_List.cshtml")");
}, 3000);

Also note that if your routing is configured properly, then you can use Url.Action() which would be more robust, eg @Url.Action("_List", "ControllerNameHere");

However, it should be noted that making an AJAX request to your server every 3 seconds is not a good idea. It doesn't scale at all, and will cause performance issues. If you need to keep the serve data and client UI in close sync, then a much better idea would be to use Server Sent Events or WebSockets.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.