我在我的mvc应用程序中使用WebGrid。
<div class="grid-div" id="webgridid">
@grid.GetHtml(
tableStyle: "gridTable",
headerStyle: "gridHead",
footerStyle: "gridFooter",
columns: new[]
{
grid.Column("name","Name", canSort: true, style: "name"),
grid.Column("description","Description", canSort: true, style: "description"),
grid.Column("duration","Duration", canSort: true, style: "duration"),
})
</div>
我可以使用表单编辑选定的行值。编辑此值后,它不会反映在我的 webGrid 中。但它反映在数据库中。为了将其反映到 webGrid 中,我需要通过从数据库加载数据来刷新 webGrid。如何从数据库将内容重新加载到 WebGrid?同样在重新加载它后,这个页面编号应该是旧的。怎么做?
我找到了解决问题的方法。我必须用ID
定义一个<div>
,并在WebGrid
控件的ajaxUpdateContainerId
属性中引用div
的id
,这将允许WebGrid
使用Ajax
异步更新数据。
<div id="ajaxgrid">
@{
var grid = new WebGrid(Model, rowsPerPage: 10, selectionFieldName: "SelectedRow", ajaxUpdateContainerId: "ajaxgrid");
}
</div>
然后,调用 WebGrid
的 GetHtml
方法,以便 Razor 引擎可以为它生成相应的HTML
。标记结束后<div>
。
@grid.GetHtml(
tableStyle: "gridTable",
headerStyle: "gridHead",
footerStyle: "gridFooter",
columns: new[]
{
grid.Column("name","Name", canSort: true, style: "name"),
grid.Column("description","Description", canSort: true, style: "description"),
grid.Column("duration","Duration", canSort: true, style: "duration"),
})
然后在我ajax
更新呼吁中,我添加了location.reload()
来刷新我的WebGrid
。
$.ajax({
url: '@Url.Action("User", "UserDetails")',
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: formData,
contentType: false,
processData: false,
success: function (result) {
if (result == "OK") {
location.reload();
}
else
alert(result);
},
error: function (result) {
alert('Error!');
}
});
现在它对我来说工作正常。