【问题标题】:MVC WebGrid - How to call a method on WebGridRow item / model?MVC WebGrid - 如何在 WebGridRow 项目/模型上调用方法?
【发布时间】:2016-04-08 21:15:38
【问题描述】:

在使用 WebGrids 时,我发现我可以访问绑定到 WebGrid 的模型上的属性,但我无法访问模型本身上的方法。

例如,这是可行的:

// Accessing a property of item works
reportGrid.Column("", header: "First Name", format: item => item.firstName )

但这不起作用: (我展示了一个简单的例子,但就我而言,我必须在 User 对象本身上调用一个方法。)

// Accessing a method on item does not work 
reportGrid.Column("", header: "First Name Backwards", format: item => item.firstNameBackwards() )

=> error: 'System.Web.Helpers.WebGridRow' does not contain a definition for 'getFullName'

这似乎与以下内容有关:

Why can't I use my extension method in a delegate in the Razor WebGrid http://www.mikesdotnetting.com/Article/171/Why-You-Can%27t-Use-Extension-Methods-With-A-WebGrid

我看不到将这些解决方案应用于我的问题的方法。正如迈克布林德所说:

WebGridColumn 的 Format 参数采用的参数是 代表:功能。这意味着你必须 传入一个动态类型,然后在它之前对其进行一些处理 作为对象返回。

...

当您尝试使用扩展方法时,编译器将检查 键入您尝试使用它以查看是否存在此类方法。如果它 没有。它将检查类型派生自的任何基类 查看它们是否包含具有正确名称的正式方法。

似乎应该找到我的方法,但可能不是因为绑定到 WebGrid 的模型实际上是一个包含 IList<T> LineItems 的分页模型,其中包含我的用户引用。

是否有任何方法可以转换或获取对 User 对象本身的引用,以便我可以调用在其上定义的方法以及访问其属性?

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4 webgrid


    【解决方案1】:

    我找到了解决这个问题的方法,但我仍然希望有更好的方法。我将不胜感激有关此或替代解决方案的任何反馈。

    在探索这个问题并检查我的其他一些 WebGrid 代码时,我发现我能够访问针对对象定义的二阶方法,这些对象可以通过绑定到 WebGrid 的模型对象上的属性来访问。

    示例(简化):

    reportGrid.Column("", header: "First Name Backwards", 
      format: item => item.BestFriend.firstNameBackwards() )
    => Works!
    

    更进一步,我将双向关系追溯到原始对象,因此我可以调用它的方法:

    // Assume all best-friend relationships are reciprocal 
    reportGrid.Column("", header: "First Name Backwards", 
      format: item => item.BestFriend.BestFriend.firstNameBackwards() )
    
    => Works!
    

    考虑到这一点,我修改了我的 User 模型以包含对其自身的引用:

        public User() {
            this._self = this; // Initialize User object with a reference to itself
        }
    
        [NotMapped]
        public User _self { get; set; }
    

    解决方案 - 现在我可以使用 _self 属性调用针对 User 模型定义的方法。

    reportGrid.Column("", header: "", 
      format: item => Helper.userTML(item._self.firstNameBackwards() ) )
    
    => Works!
    

    【讨论】:

      【解决方案2】:

      在探索这个问题并检查我的其他一些 WebGrid 代码时,我发现我能够访问针对对象定义的二阶方法,这些对象可以通过绑定到 WebGrid 的模型对象上的属性来访问。

      我遇到了同样的问题,根据这条评论,我发现这是因为我没有公开模型的数据成员。将您的 firstName 定义设置为 public,这将解决它。

      【讨论】:

        【解决方案3】:

        我发现最适合我的方法是在模型类中使用 getter。

        在您的模型类(用户?)中:

        public string firstNameBackwards
        {
            get 
            { 
                var firstNameBackwards = firstName;
                // Do something here
                return firstNameBackwards; 
            }
        }
        

        在您的网络网格中:

        reportGrid.Column("", header: "First Name Backwards", format: item => item.firstNameBackwards )
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-18
          • 2020-05-03
          • 1970-01-01
          • 2016-02-17
          • 2017-08-25
          • 1970-01-01
          相关资源
          最近更新 更多