英文:
asp-route-xxx in mvc and unobtrusive ajax not working
问题
我有一系列使用Ajax链接('a'标签)的链接,如下所示:
@foreach (int i in Enumerable.Range(1, Model.PagingInfo.TotalPages))
{
string aClass = (Model.PagingInfo.CurrentPage == i ? "btn btn-secondary" : "btn btn-primary");
<li class="page-item @(Model.PagingInfo.CurrentPage == i ? "active" : "")">
<a class="page-link"
data-ajax="true" data-ajax-method="get"
data-ajax-url="@(Url.Action("GetUsers", "AccountAdmin"))"
data-ajax-mode="replace" data-ajax-update="#TableDiv"
data-ajax-loading="#ajax" data-ajax-failure="failed"
asp-route-page="@i"
asp-route-pageSize="@Model.PageSize"
asp-route-search="@Model.Search"
asp-route-sortOrder="@Model.SortOrder"
asp-route-sortExpression="@Model.SortExpression">
@i
</a>
</li>
}
当我检查第二页链接时,asp-route变量被编码在Ajax链接的href属性中。href是正确的,对于第二个链接,它们的值是正确的。
但是当我点击第二个链接时,没有路由值传递到操作方法,并且操作方法以默认值执行。
如何解决这个问题?
英文:
I have a series of links using ajax link ('a' tag) like this:
@foreach (int i in Enumerable.Range(1, Model.PagingInfo.TotalPages))
{
string aClass = (Model.PagingInfo.CurrentPage == i ? "btn btn-secondary" : "btn btn-primary");
<li class="page-item @(Model.PagingInfo.CurrentPage == i ? "active" : "")">
<a class="page-link"
data-ajax="true" data-ajax-method="get"
data-ajax-url="@(Url.Action("GetUsers", "AccountAdmin"))"
data-ajax-mode="replace" data-ajax-update="#TableDiv"
data-ajax-loading="#ajax" data-ajax-failure="failed"
asp-route-page="@i"
asp-route-pageSize="@Model.PageSize"
asp-route-search="@Model.Search"
asp-route-sortOrder="@Model.SortOrder"
asp-route-sortExpression="@Model.SortExpression">
@i
</a>
</li>
}
when I inspect page 2 link, The asp-route variables are encoded in the ajax link's href property. the href is correct and for 2nd link there are correct values.
but when I click the 2nd link, no route value come to the action method. and the action method is executed with default values.
How to solve this?
答案1
得分: 0
我将"a"更改为:
<a class="page-link"
data-ajax="true" data-ajax-method="get"
data-ajax-url="@(Url.Action("GetUsers", "AccountAdmin",
new { page = i, pageSize = Model.PageSize, search = Model.Search,
sortOrder = Model.SortOrder, sortExpression = Model.SortExpression}))"
data-ajax-mode="replace" data-ajax-update="#TableDiv"
data-ajax-loading="#ajax" data-ajax-failure="failed">
@i
</a>
现在可以正常工作。
英文:
I changed the a to:
<a class="page-link"
data-ajax="true" data-ajax-method="get"
data-ajax-url="@(Url.Action("GetUsers", "AccountAdmin",
new { page = i, pageSize = Model.PageSize, search = Model.Search,
sortOrder = Model.SortOrder, sortExpression = Model.SortExpression}))"
data-ajax-mode="replace" data-ajax-update="#TableDiv"
data-ajax-loading="#ajax" data-ajax-failure="failed">
@i
</a>
And now works.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论