gpt4 book ai didi

javascript - MVC3 中使用 ajax 进行 Webgrid 刷新

转载 作者:行者123 更新时间:2023-11-28 02:07:06 24 4
gpt4 key购买 nike

我有一个网络网格,按下“刷新”按钮时需要刷新。我还有一个搜索输入。

一切工作正常,除了每次我点击刷新时,pageNumber 都会被设置回 1...

这是我的代码...

Controller

public ActionResult ListImporting(string searchText = "", int page = 1)
{
ViewBag.RowsPerPage = 2;

searchText = searchText.Trim();
ViewBag.searchText = searchText;
ViewBag.page = page;

DtoPaginatedResult<ListImportingDTO> model = listService.GetListsInProgress(page, ViewBag.RowsPerPage, searchText);

if (Request.IsAjaxRequest())
return PartialView("ListImportingGrid", model);
else
return View(model);

}

然后我有一个 View 列表导入,它调用部分...

<input id="refreshButton" type="button" value="Refresh" style="float:right"/>

<div id="resultList" style="margin-top:20px">
@Html.Partial("ListImportingGrid", Model)
</div>

……

   $("#refreshButton").live("click",updateGrid);

在部分内部我有网格和当前函数

function updateGrid() {     
var pageNumber = @ViewBag.page;
console.log(pageNumber);
$.ajax(
{ type: "GET" ,
url: '/Admin/ListImporting/',
data: { searchText: $("#searchBox").val(),
page: pageNumber
} ,
dataType: "html" ,
success: function (data){
$("#resultList").html(data);
}
})
}

最佳答案

加载页面时,您将页码硬编码到 ViewBag.page,页面加载时该页码始终等于 1。

相反,您需要做的是在分部 View 中创建一个隐藏输入并将 JavaScript 代码放置在主视图中。

然后,当 updateGrid 函数被触发时,通过 JavaScript 在空间 View 中查找,并找到页码隐藏字段中的值。

切换到PagedList,使用ajax、分页和网络网格要容易得多。 https://github.com/TroyGoode/PagedList

关于javascript - MVC3 中使用 ajax 进行 Webgrid 刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17616971/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com