Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将Html.DisplayNameFor与IEnumerable一起使用<;i分组>;_C#_Linq_Razor_Asp.net Core Mvc - Fatal编程技术网

C# 将Html.DisplayNameFor与IEnumerable一起使用<;i分组>;

C# 将Html.DisplayNameFor与IEnumerable一起使用<;i分组>;,c#,linq,razor,asp.net-core-mvc,C#,Linq,Razor,Asp.net Core Mvc,我在Razor页面中从显示值表(IEnumerable)切换到显示分组的值表(IEnumerable) 获取模型属性的显示名称的语法是@Html.DisplayNameFor(model=>model.ID) 我认为我没有正确的语法将DisplayNameFor与I分组一起使用,因为它只显示模型的一个属性(在本例中为ID):@Html.DisplayNameFor(item=>item.FirstOrDefault().ElementAtOrDefault(0.ID) 正确的语法是什么?我认为你

我在Razor页面中从显示值表(
IEnumerable
)切换到显示分组的值表(
IEnumerable

获取模型属性的显示名称的语法是
@Html.DisplayNameFor(model=>model.ID)

我认为我没有正确的语法将
DisplayNameFor
I分组一起使用,因为它只显示模型的一个属性(在本例中为
ID
):
@Html.DisplayNameFor(item=>item.FirstOrDefault().ElementAtOrDefault(0.ID)


正确的语法是什么?

我认为你的语法是正确的

@model IEnumerable<IGrouping<DateTime, FooViewModel>>

@if (Model.Any())
{
    /*
     * Since we've checked there are groupings, you can just use .First() to get 
     * the first group.
     *
     * .Key will give you the key you use to group things by. In this case, it's 
     * DateTime.
     *
     * Since there are groupings, there must be elements in the group so it's safe 
     * to use .ElementAt() to get to the view model, FooViewModel.
     */

    <table class="table">
        <thead>
            <tr>
                <th>@Html.DisplayNameFor(model => model.First().Key)</th>
                <th>@Html.DisplayNameFor(model => model.First().ElementAt(0).ID)</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var group in Model)
            {
                <tr>
                    <td>@group.Key</td>
                    <td>@String.Join(", ", group.Select(x => x.ID))</td>
                </tr>
            }
        </tbody>
    </table>
}
@model IEnumerable
@if(Model.Any())
{
/*
*因为我们已经检查了是否存在分组,所以您可以使用.First()获取
*第一组。
*
*.Key将为您提供用于分组的键。在这种情况下,它是
*日期时间。
*
*因为有分组,所以组中必须有元素,这样才安全
*要使用.ElementAt()访问视图模型,请选择FooViewModel。
*/
@DisplayNameFor(model=>model.First().Key)
@DisplayNameFor(model=>model.First().ElementAt(0.ID)
@foreach(模型中的var组)
{
@组键
@String.Join(“,”,group.Select(x=>x.ID))
}
}
还有假数据:

公共类FooViewModel
{
[显示(Name=“日期时间”)]
公共日期时间日期时间{get;set;}
[显示(Name=“Haha ID”)]
公共int ID{get;set;}
}
var数据=新列表
{
new FooViewModel{DateTime=new DateTime(2019,06,25),ID=1},
new FooViewModel{DateTime=new DateTime(2019,06,25),ID=2},
new FooViewModel{DateTime=new DateTime(2019,06,24),ID=3},
new FooViewModel{DateTime=new DateTime(2019,06,23),ID=4}
};
它应该是这样的:


如何使用
first或default
ElementAtOrDefault
DisplayName for
是否总是只显示一个属性的DisplayName?你希望它做什么?你的问题不清楚。“它只显示模型的一个属性”-是的,它就是这样做的。不能在模型级别使用
DisplayNameFor
以某种方式呈现所有属性的名称。这不是它的工作原理。我犯了一个错误——语法是正确的。投票结束。呃。
FooViewModel
有其他属性(例如
Name
StartingDate
)没有显示。@craig:你不能添加更多标题为
@Html.DisplayNameFor(model=>model.First().ElementAt(0.Name)
@Html.DisplayNameFor(model=>model.First().ElementAt(0)的列吗.StartingDate)
等?就像@Chris Pratt说的,你的问题不清楚,所以我猜你在找什么。