我在一页上有两个表,这两个表之一是根据时间更新的,我只需要更新允许更新的表即可。 但 以下代码的输出如下:
代码:
public IActionResult Pre(int id)
{
var model = new Test();
model.cart = (from a in _context.liveSports
join b in _context.odds on a.Id equals b.GameId
select new Cart_test()
{
AwayTeam = a.AwayTeam,
HomeTeam = a.HomeTeam,
}).Take(40).ToList();
model.sport = (from a in _context.sport
select new Sport_test()
{
SportName = a.sport_Name,
}).Take(20).ToList();
return View(model);
}
上面的代码用于Homecontroller
查看:
<div class="body row" style="background-color:#1e1e1e;">
<div class="col-lg-5" id="NotRefresh">
<table class="table" style="color:white">
<thead>
<tr>
<th scope="col">home</th>
<th scope="col">away</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.cart)
{
<tr>
<td>@item.HomeTeam</td>
<td>@item.AwayTeam</td>
</tr>
}
</tbody>
</table>
</div>
<div class="col-lg-5" id="Refresh">
<table class="table" style="color:white;background-color:#808080">
<thead>
<tr>
<th scope="col">name sport</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.sport)
{
<tr>
<td>@item.SportName</td>
</tr>
}
</tbody>
</table>
</div>
脚本:
$(document).ready(function () {
setTimeout(function () {
$("#Refresh").load('@Url.Action( "Pre","Home") ');
}, 2300);
});
我可能需要使用Ajax进行更新。
但是
我不知道如何从控制器获取数据并仅更新ID为:Refresh的div
答案 0 :(得分:0)
向表添加ID:
<table class="table" ID="RefreshTable" style="color:white;background-color:#808080">..
然后,您可以仅询问该元素及其内容:
$(document).ready(function () {
setTimeout(function () {
$("#Refresh").load('@Url.Action( "Pre","Home") #RefreshTable');
}, 2300);
});
答案 1 :(得分:0)
添加了部分视图 查看更改:
$(document).ready(function () {
setTimeout(function () {
$("#Refresh").load('@Url.Action( "test","Home") ');
}, 2300);
});
<div class="body row" style="background-color:#1e1e1e;">
<div class="col-lg-5" id="NotRefresh">
<table class="table" style="color:white">
<thead>
<tr>
<th scope="col">home</th>
<th scope="col">away</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.cart)
{
<tr>
<td>@item.HomeTeam</td>
<td>@item.AwayTeam</td>
</tr>
}
</tbody>
</table>
</div>
@Html.Partial("_test", Model)
</div>
_test.cshtml:
$(document).ready(function () {
setTimeout(function () {
$("#Refresh").load('@Url.Action( "test","Home") ');
}, 2300);
});
@model Winigoo_web.Models.ViewModels.Test
<div class="col-lg-5">
<table class="table" style="color:white;background-color:#808080" id="Refresh">
<thead>
<tr>
<th scope="col">name sport</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.sport)
{
<tr>
<td>@item.SportName</td>
</tr>
}
</tbody>
</table>
</div>
还有
添加一项操作以返回部分视图,例如:
public IActionResult test(int id)
{
var model = new Test();
model.cart = (from a in _context.liveSports
join b in _context.odds on a.Id equals b.GameId
select new Cart_test()
{
AwayTeam = a.AwayTeam,
HomeTeam = a.HomeTeam,
}).Take(40).ToList();
model.sport = (from a in _context.sport
select new Sport_test()
{
SportName = a.sport_Name,
}).Take(20).ToList();
return PartialView("_test",model);
}
其核心工作