asp-route-xxx在MVC和无侵入式Ajax中不起作用。

huangapple go评论54阅读模式
英文:

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 ? &quot;btn btn-secondary&quot; : &quot;btn btn-primary&quot;);
    &lt;li class=&quot;page-item @(Model.PagingInfo.CurrentPage == i ? &quot;active&quot; : &quot;&quot;)&quot;&gt;
        &lt;a class=&quot;page-link&quot;
            data-ajax=&quot;true&quot; data-ajax-method=&quot;get&quot; 
            data-ajax-url=&quot;@(Url.Action(&quot;GetUsers&quot;, &quot;AccountAdmin&quot;))&quot;
            data-ajax-mode=&quot;replace&quot; data-ajax-update=&quot;#TableDiv&quot;
            data-ajax-loading=&quot;#ajax&quot; data-ajax-failure=&quot;failed&quot; 
            asp-route-page=&quot;@i&quot; 
            asp-route-pageSize=&quot;@Model.PageSize&quot;
            asp-route-search=&quot;@Model.Search&quot; 
            asp-route-sortOrder=&quot;@Model.SortOrder&quot;
            asp-route-sortExpression=&quot;@Model.SortExpression&quot;&gt;                                       
            @i
        &lt;/a&gt;
    &lt;/li&gt;                               
}

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"更改为:

 &lt;a class=&quot;page-link&quot;
     data-ajax=&quot;true&quot; data-ajax-method=&quot;get&quot; 
     data-ajax-url=&quot;@(Url.Action(&quot;GetUsers&quot;, &quot;AccountAdmin&quot;, 
                    new { page = i, pageSize = Model.PageSize, search = Model.Search,
                    sortOrder = Model.SortOrder, sortExpression = Model.SortExpression}))&quot;
     data-ajax-mode=&quot;replace&quot; data-ajax-update=&quot;#TableDiv&quot;
     data-ajax-loading=&quot;#ajax&quot; data-ajax-failure=&quot;failed&quot;&gt;                                  

     @i
 &lt;/a&gt;

现在可以正常工作。

英文:

I changed the a to:

 &lt;a class=&quot;page-link&quot;
     data-ajax=&quot;true&quot; data-ajax-method=&quot;get&quot; 
     data-ajax-url=&quot;@(Url.Action(&quot;GetUsers&quot;, &quot;AccountAdmin&quot;, 
                    new { page = i, pageSize = Model.PageSize, search = Model.Search,
                    sortOrder = Model.SortOrder, sortExpression = Model.SortExpression}))&quot;
     data-ajax-mode=&quot;replace&quot; data-ajax-update=&quot;#TableDiv&quot;
     data-ajax-loading=&quot;#ajax&quot; data-ajax-failure=&quot;failed&quot;&gt;                                  

     @i
 &lt;/a&gt;

And now works.

huangapple
  • 本文由 发表于 2023年7月23日 23:00:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76748917.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定