![](/img/trans.png)
[英]Refreshing MVC PartialView with new Model on DropDownList Change
[英]MVC PartialView is not refreshing data
我有一個索引頁面:
@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之后,我無法在局部視圖(_VentasTable)中刷新數據(不在HTML.Partial中,請注意我的代碼中有注釋。)在我的部分放置斷點之后view我看到數據從數據庫查詢中變為CHANGED,但是這個數據在部分視圖中沒有被替換。 有什么幫助嗎?
正如@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);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.