单个视图上的多个WebGrid更新任何操作上的所有WebGrid

本文关键字:WebGrid 任何 操作上 更新 视图 单个 | 更新日期: 2023-09-27 17:58:19

我在一个单一视图上有两个使用相同模型的WebGrid。现在的问题是,每当我在一个网络网格上应用排序时,它也在对另一个网络栅格进行排序。我使用的fieldNamePrefix仍然有问题。这是我的代码:

第一部分视图:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<CRMEvent.Models.CRM.DatabaseEntities.CRM_Dashboard_Requests>>" %>
<%    
     var grid = new WebGrid(source: Model, defaultSort: "CRM_ID",fieldNamePrefix:"grid2", canSort: true);%>
<%=
        grid.GetHtml( htmlAttributes: new { id = "grid2" },tableStyle: "GridTable", headerStyle: "GridHeader", columns: grid.Columns(
          grid.Column(columnName: "CRM_ID", header: "CRM ID", canSort: true),
         grid.Column(columnName: "MDLNO", header: "MDLNO", canSort: true),
         grid.Column(columnName: "EMP_Name", header: "Employee", canSort: true),
         grid.Column(columnName: "SERVICE_TYPE", header: "Service Type", canSort: true),
         grid.Column(columnName: "Requested_Amount", header: "Amount", canSort: true),
                 grid.Column(columnName: "Created_Date", header: "Date", canSort: true, format: item => item.Created_Date.ToString("dd-MM-yyyy"))
         ))
   %>

第二部分视图:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<ApricaCRMEvent.Models.CRM.DatabaseEntities.CRM_Dashboard_Requests>>" %>
 <%    
     var grid = new WebGrid(source: Model, defaultSort: "CRM_ID",fieldNamePrefix:"grid1", canSort: true);%>
     <%=
        grid.GetHtml( htmlAttributes: new { id = "grid1" },tableStyle: "GridTable", headerStyle: "GridHeader", columns: grid.Columns(
          grid.Column(columnName: "CRM_ID", header: "CRM ID", canSort: true),
         grid.Column(columnName: "MDLNO", header: "MDLNO", canSort: true),
         grid.Column(columnName: "EMP_Name", header: "Employee", canSort: true),
         grid.Column(columnName: "SERVICE_TYPE", header: "Service Type", canSort: true),
         grid.Column(columnName: "Requested_Amount", header: "Amount", canSort: true),
                 grid.Column(columnName: "Created_Date", header: "Date", canSort: true, format: item => item.Created_Date.ToString("dd-MM-yyyy"))
         ))
   %>

单个视图上的多个WebGrid更新任何操作上的所有WebGrid

在webgrid中有一个名为"ajaxUpdateContainerId"的属性。此属性可以帮助您解决问题。

第一部分视图

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<CRMEvent.Models.CRM.DatabaseEntities.CRM_Dashboard_Requests>>" %>
<%    
 var grid1 = new WebGrid(source: Model, defaultSort: "CRM_ID",fieldNamePrefix:"grid2", canSort: true, ajaxUpdateContainerId:"FirstDiv");%>
<div id="FirstDiv">
<%=
    grid1.GetHtml( htmlAttributes: new { id = "grid2" },tableStyle: "GridTable", headerStyle: "GridHeader", columns: grid.Columns(
      grid1.Column(columnName: "CRM_ID", header: "CRM ID", canSort: true),
     grid1.Column(columnName: "MDLNO", header: "MDLNO", canSort: true),
     grid1.Column(columnName: "EMP_Name", header: "Employee", canSort: true),
     grid1.Column(columnName: "SERVICE_TYPE", header: "Service Type", canSort: true),
     grid1.Column(columnName: "Requested_Amount", header: "Amount", canSort: true),
             grid1.Column(columnName: "Created_Date", header: "Date", canSort: true, format: item => item.Created_Date.ToString("dd-MM-yyyy"))
     ))
%>
</div>

第二部分视图

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<ApricaCRMEvent.Models.CRM.DatabaseEntities.CRM_Dashboard_Requests>>" %>
 <%    
 var grid = new WebGrid(source: Model, defaultSort: "CRM_ID",fieldNamePrefix:"grid1", canSort: true, ajaxUpdateContainerId:"SecondDiv");%>
 <div id="SecondDiv">
<%=
    grid.GetHtml( htmlAttributes: new { id = "grid1" },tableStyle: "GridTable", headerStyle: "GridHeader", columns: grid.Columns(
      grid.Column(columnName: "CRM_ID", header: "CRM ID", canSort: true),
     grid.Column(columnName: "MDLNO", header: "MDLNO", canSort: true),
     grid.Column(columnName: "EMP_Name", header: "Employee", canSort: true),
     grid.Column(columnName: "SERVICE_TYPE", header: "Service Type", canSort: true),
     grid.Column(columnName: "Requested_Amount", header: "Amount", canSort: true),
             grid.Column(columnName: "Created_Date", header: "Date", canSort: true, format: item => item.Created_Date.ToString("dd-MM-yyyy"))
     ))
  %>
  </div>

您可能不应该在两个网格上使用相同的var名称。试试var gridA和var gridB。