C# MVC PartialView未刷新数据
我有一个索引页:C# MVC PartialView未刷新数据,c#,asp.net-mvc,asp.net-mvc-4,asp.net-mvc-partialview,renderpartial,C#,Asp.net Mvc,Asp.net Mvc 4,Asp.net Mvc Partialview,Renderpartial,我有一个索引页: @model AlfoncinaMVC.Models.VentaIndexViewModel @{ ViewBag.Title = "Ventas"; Layout = "~/Views/Shared/_Layout.cshtml"; } <script> var d = 1; setInterval(function () { d++; $('#testLabe').text(d);
@model AlfoncinaMVC.Models.VentaIndexViewModel
@{
ViewBag.Title = "Ventas";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<script>
var d = 1;
setInterval(function () {
d++;
$('#testLabe').text(d);
$.ajax("Ventas");
}, 1000 * 1 * 1);
</script>
<div id="ventasTable">
@{ Html.RenderPartial("_VentasTable"); }
@*@Html.Partial("_VentasTable")*@
</div>
<label id="testLabe"></label>
使用局部视图_VentasTable:
@model AlfoncinaMVC.Models.VentaIndexViewModel
<table>
<thead>
</thead>
<tbody>
@foreach (var item in @Model.Ventas)
{
<tr>
<td>
@item.nombreArticulo
</td>
</tr>
}
</tbody>
</table>
使用此控制器:
public ActionResult Ventas()
{
var db = new AlfonsinaEntities();
var ventas = db.Set<Venta>();
var vm = new VentaIndexViewModel
{
Ventas = ventas.Select(x => new VentaViewModel
{
nombreArticulo = x.NombreArticulo
}).ToList()
};
if (Request.IsAjaxRequest())
{
return PartialView("_VentasTable", vm);
}
return View("Ventas", vm);
}
在调用Html.RenderPartial之后,我无法在部分视图中获取要刷新的数据,也不能使用Html.partial,请注意,在我的代码中有注释。
在我的部分视图上放置断点后,我看到数据已从DB查询中更改,但该数据未在部分视图中被替换。有什么帮助吗?正如@StephenMuecke所说的-您需要将返回的数据添加到DOM中:
$.ajax({
type: "GET",
url: '@Url.Action("Ventas", "ControllerName")',
async: true,
cache: false,
dataType: "html",
success: function (data, textStatus, jqXHR) {
$("#ventasTable").html(data);
},
error: function (jqXHR, textStatus, errorThrown) {
alert(textStatus + " - " + errorThrown);
}
});
什么是$.ajaxVentas;应该实现什么?阅读本文为@Igor的评论提供更多的上下文,您不能只向jquery的ajax调用提供ActionResult的名称。这样不行。@Igor对不起,你是什么意思?我没有发现我的呼叫有任何问题,它正在到达控制器。你需要阅读@Igor给你的链接。您需要在成功回调中将返回的数据添加到DOM中。1000*1*1始终为1000有什么意义?建议您使用url:“@url.ActionVentas,ControllerName”,以确保url正确无误。还值得一提的是,脚本应位于页面底部或包装在文档中。就绪