WebGrid 中的复杂类型

发布于 2024-12-02 09:33:04 字数 464 浏览 1 评论 0原文

令人惊讶的是,我在过去 3 小时一直在互联网上搜索如何从作为数据源的复杂类型绑定 webgrid 列。但我找不到任何有用的信息。 有一个关于 MVC 3 中的复杂 WebGrid 绑定 的主题,但没有在我的场景中工作。

为了简化它,假设我有一个 Employee 对象列表,我从中填充 WebGrid,每个 Employee 都有 Address 属性,该属性是 Address 类型。

我想在 WebGrid 中显示“地址”字段的“城市”属性以及“员工”的其他字段。

我认为 grid.Column("Address.City") 可以工作,但事实并非如此。这是不支持的事情,还是我做错了什么。

感谢您的帮助。

问候

无政府主义者极客

I have, surprisingly, been searching on the internet how to bind a webgrid column from a complex type that is data source last 3 hours. But I could not find any useful information.
there is a topic on Complex WebGrid Binding in MVC 3, but it did not work in my scenario.

To simplify it, let's say I have a list of Employee objects that I populate WebGrid from, each Employee have Address property which is Address type.

I would like to show the City property of Address field in the WebGrid along with other fields of Employee.

I assumed that grid.Column("Address.City") would work, but it does not. Is that something not supported, or I am doing something wrong.

Thanks for your help.

Regards

AnarchistGeek

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

衣神在巴黎 2024-12-09 09:33:04

我无法看到您的答案是如何解决问题的,直到我意识到我缺少的是有时该属性可以为空,但您得到的不是空引用错误,而是错误列“Address.City”不存在.除非您在格式属性中检查 null ....我在这里找到了答案

@functions{
        public class Employee
        {
            public string Name { get; set; }
            public Address Address { get; set; }
        }
        public class Address
        {
            public string City { get; set; }
        }
    }
    @{
        var myClasses = new List<Employee>{
              new Employee   { Name="A" , Address = new Address{ City="AA" }},
              new Employee   { Name="B" , Address = new Address{ City="BB" }},
              new Employee   { Name="C" , Address = new Address{ City=null }},
              new Employee   { Name="D" , Address = null},
    };
        var grid = new WebGrid(source: myClasses);
    }
    @grid.GetHtml(
    columns: grid.Columns(grid.Column("Address.City",
    header: "City",
    format: @<text>@if (item.Address != null)
                   {@item.Address.City}
    </text>), 
    grid.Column("Name")))

I could not see how your answer was fixing the issue until I realised that what I was missing is that sometimes the property can be null but instead of a null reference error you get the error Column "Address.City" does not exist. unless you check for null in the format property....I found the the answer here

@functions{
        public class Employee
        {
            public string Name { get; set; }
            public Address Address { get; set; }
        }
        public class Address
        {
            public string City { get; set; }
        }
    }
    @{
        var myClasses = new List<Employee>{
              new Employee   { Name="A" , Address = new Address{ City="AA" }},
              new Employee   { Name="B" , Address = new Address{ City="BB" }},
              new Employee   { Name="C" , Address = new Address{ City=null }},
              new Employee   { Name="D" , Address = null},
    };
        var grid = new WebGrid(source: myClasses);
    }
    @grid.GetHtml(
    columns: grid.Columns(grid.Column("Address.City",
    header: "City",
    format: @<text>@if (item.Address != null)
                   {@item.Address.City}
    </text>), 
    grid.Column("Name")))
浮华 2024-12-09 09:33:04

这是我在 ASP.NET 论坛中得到的答案,我想将其发布在这里,以防其他人可能需要。

线程链接是 http://forums.asp.net/p/1718114/4586676.aspx/1?Re%20Complex%20data%20type%20in%20WebGrid

解决方案是(经过测试):

@functions{
    public class Employee
    {
        public string Name { get; set; }
        public Address Address { get; set; }
    }
    public class Address
    {
        public string City { get; set; }
    }
}
@{
    var myClasses = new List<Employee>{
          new Employee   { Name="A" , Address = new Address{ City="AA" }},
          new Employee   { Name="B" , Address = new Address{ City="BB" }},
          new Employee   { Name="C" , Address = new Address{ City="CC" }},
          new Employee   { Name="D" , Address = new Address{ City="DD" }},
};
    var grid = new WebGrid(source: myClasses);
}
@grid.GetHtml(
columns: grid.Columns(grid.Column("Address.City",header:"City"), grid.Column("Name")))

我希望它对其他人有帮助。

干杯。

This is the answer I got in ASP.NET forums and I wanted post it here in case others may need that.

The thread link is http://forums.asp.net/p/1718114/4586676.aspx/1?Re%20Complex%20data%20type%20in%20WebGrid

and the solution is(Tested):

@functions{
    public class Employee
    {
        public string Name { get; set; }
        public Address Address { get; set; }
    }
    public class Address
    {
        public string City { get; set; }
    }
}
@{
    var myClasses = new List<Employee>{
          new Employee   { Name="A" , Address = new Address{ City="AA" }},
          new Employee   { Name="B" , Address = new Address{ City="BB" }},
          new Employee   { Name="C" , Address = new Address{ City="CC" }},
          new Employee   { Name="D" , Address = new Address{ City="DD" }},
};
    var grid = new WebGrid(source: myClasses);
}
@grid.GetHtml(
columns: grid.Columns(grid.Column("Address.City",header:"City"), grid.Column("Name")))

I hope it helps others.

Cheers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文