【问题标题】:MVC WebGrid paging action changing upon navigationMVC WebGrid 分页操作在导航时发生变化
【发布时间】:2023-03-15 22:47:01
【问题描述】:

我正在使用带有分页的 webgrid 的 MVC 应用程序。网格本身在名为 _Results.cshtml 的局部视图中呈现,并使用

在 index.cshtml 页面上的 div 中呈现
Html.RenderPartial("_Results", Model.Results);

index.cshtml 上的部分网格以及其他一些表单控件被包装在一个名为 ResultsAction 的表单中,使用:

@using (Ajax.BeginForm("ResultsAction", "Results", new AjaxOptions.....

当最初导航到 index.cshtml 时,网格会按预期填充,并且将鼠标悬停在任何分页链接上都会正确显示:

http://localhost/ResultsAction?page=<page#>

网格中的每一行都有一个指向详细信息页面的链接。这可以按预期工作,并且详细信息页面有一个链接可以使用以下命令返回结果网格:

@Html.ActionLink("Return To Results", "Index", "Results")

现在的问题。这将我重定向回 Index.cshtml 就好了,但现在当我将鼠标悬停在网格中的任何分页链接上时,它们错误地使用:

http://localhost/Index?page=<page#>

这是错误的控制器操作,因此分页不再起作用。我的理解是分页链接应该使用表单名称作为操作发出 Get,但是当我导航到详细信息然后再次返回时,它会以某种方式被覆盖。有谁知道是什么导致了这种行为,或者我如何指定分页链接以始终使用相同的控制器操作?

编辑:按要求发布部分视图的代码:

@model IEnumerable<ispPR_GetInquiryRecords_Result>

@{
    Layout = null;
}

<input id="searchListCnt" type="hidden" value="@Model.Count()" />

<div id="gridSearch">

@{

    var grid = new WebGrid(selectionFieldName: "SelectedRow", canSort: false, canPage: true, rowsPerPage: 10, ajaxUpdateContainerId: "gridSearch");

var virtualCount = Model != null && Model.Count() > 0 ? Model.First().VirtualCount : 0;

grid.Bind(Model, rowCount: (int)virtualCount, autoSortAndPage: false);
}

    <div id="gridContent">
    @grid.GetHtml(
    htmlAttributes: new { id = "inqgrid" },
    tableStyle: "webGrid",
                  fillEmptyRows: false,
                  footerStyle: "gridFooter",
                  displayHeader: true,
                  alternatingRowStyle: "alt",
                  selectedRowStyle: "select",
                  mode: WebGridPagerModes.All,
                  columns: grid.Columns(
                  grid.Column("PriceStatus",header:"Price Status"),
                      grid.Column("CustomerName","Customer Name"),
                      grid.Column("EndUserName", "End User"),
                      grid.Column("ContractNumber","Contract"),
                      grid.Column("PriceLevel", "Level"),
                      grid.Column("ProductDescription", "Product Code"),


                      grid.Column(

                                            header: "Break Qty",                           
                                            format: @<text>@item.QuantityBreak.ToString() / @item.QuantityBreakUOM </text>

                                        ),

                      grid.Column("BeginDate", "Begin Date", format: item =>string.Format("{0:d}", item.BeginDate)),
                      grid.Column("EndDate","End Date",format: item =>string.Format("{0:d}", item.EndDate)),

                        grid.Column(
                                        header: "Price in PricingUOM",
                                        format: item =>
                                            {
                                                var res = Html.FormatToDecimals((decimal)item.PriceInPricingUOM, (int)item.Numdecimals);

                                                switch ((bool)@item.HasDetail)
                                                {
                                                    case true:


                                                        return Html.ActionLink(res + " / " + (string)item.PricingUOM, "InquiryDetails", new { @id = @item.PriceMasterID }, new { @class = "item-link2", @id = "lnk_" + @item.PriceMasterID });
                                                    case false:
                                                        return Html.ActionLink(res+ " / " + (string)item.PricingUOM, null, null, new { onclick = "return NoDetailsDialog('" + @item.NoDetailReason + "')" });


                                                }

                                                return null;
                                            }
                                            ),

                      grid.Column(
                                            header: "Price Alt UOM",
                                            format: @<text>@Html.FormatToDecimals((decimal)item.PriceInOrderUOM, (int)item.Numdecimals) / @item.OrderUOM </text>

                                        ),

                      grid.Column("Rolling12", "Rolling 12 Sales", format: @<text>@String.Format("{0:c0}", @item.Rolling12) </text>),
                      grid.Column("CMPercent", "Net CM ", format: @<text>@String.Format("{0:0.00} %", @item.CMPercent * 100) </text>)


    ))


</div>




</div>

<script type="text/javascript">
    function NoDetailsDialog(message) {

        alert(message);
        return false;
    }
</script>

【问题讨论】:

  • 可以发一下局部视图吗?
  • 已发布。已编辑的问题。
  • WebGrid 给我带来了很多问题,我放弃了使用它。
  • 有什么好的选择?
  • @AS2012 如果您有机会查看该问题,请告诉我?

标签: asp.net-mvc asp.net-mvc-4 asp.net-mvc-3 asp.net-mvc-5


【解决方案1】:

您可以使用数据表。请让我知道您在数据表方面的进展情况,我可以帮助您完成它, 我什至可以协助使用剃刀语法:

nuget DataTabes.net jquery 插件

bundles.Add(new StyleBundle("~/Content/CssGrid").Include(
                    "~/Content/DataTables/css/jquery.dataTables.min.css"));

bundles.Add(new ScriptBundle("~/bundles/JSGrid").Include(
                    "~/Scripts/DataTables/jquery.dataTables.min.js"));

JavaScript:

//perform tasks like initialize fields, show popup, and post to server
function DeleteMe()
function EditMe()
function Add()

页面:

$(document).ready(function() {
    $('#theDataTable').DataTable();

} );

</script>

//button to show popup for add/edit here
<table id="theDataTable" class="display table table-striped table-bordered">
                <thead>
                    <tr>
                        <th>Field Name A
                        </th>
                        <th>Field Name B
                        </th>
                        <th>Field Name C
                        </th>
                         </th>
                        <th>Delete
                        </th>
                        <th>Edit
                        </th>
                </thead>
                <tbody>
                    <%  int rowCount = 0;
                        foreach (AClass item in Model.AClassList)
                        { %>
                    <tr id="<%:rowCount%>">
                        <td><%:item.FieldA%></td>
                        <td><%:item.FieldB%></td>
                        <td><%:item.FieldC%></td>
                        <td>
                            <a href="#" title="Delete" class="btn btn-default btn-xs btn-block"
                                onclick="return DeleteMe('<%:item.Id%>')">Delete</a>
                        </td>
                        <td>
                            <a href="#" title="Edit" class="btn btn-default btn-xs btn-block"
                                onclick="return EditMe('', 
                                     '<%:item.FieldA %>',
                                     '<%: Html.Encode(item.FieldB) %>', 
                                     '<%: Html.Encode(item.FieldC) %>')">Edit</a>
                        </td>
                        <% rowCount++;
                        } %>
                    </tr>
                </tbody>
            </table>

【讨论】:

  • @AS2012 你能告诉我你是否想一次加载数千个记录块,没有延迟也没有帖子?代码不同,我随时为您提供帮助。
【解决方案2】:

我以前也遇到过同样的问题。在我的情况下,它最终与 MVC 视图解析器被限定到错误的文件夹有关,因为调用的控制器与用于构造我正在调用的视图的控制器不同。

我知道这并没有多大帮助,而且您在 BeginForm 语句中明确说明了控制器名称,这看起来确实很奇怪。最后我让我的导师为我解决了这个问题,他通过反复试验完成了,只是注释掉了各个行,直到问题被隔离。

【讨论】:

    猜你喜欢
    • 2014-11-12
    • 2012-05-14
    • 1970-01-01
    • 2021-06-10
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 2012-06-21
    相关资源
    最近更新 更多