2017-06-19 69 views
0

我有以下代码:ASP.NET - HTML.Display不显示值

@model IEnumerable<SampleMvcApp.Models.Exercise> 



@foreach (var item in Model.GroupBy(m => m.DayName).Distinct()) 

{ 
    <table class="table"> 
    <h2>@Html.Display(item.Select(x => x.DayName).ToString())</h2> 
     <thead> 
      <tr> 
       <th> 
        ExerciseName 
       </th> 
       <th> 
        ExerciseTime 
       </th> 
       <th> 
        ExerciseRepetition 
       </th> 
       <th> 
        MomentOfTheDay 
       </th> 
       <th> 
        Routine 
       </th> 
       <th> 
        Week 
       </th> 
      </tr> 
     </thead> 
     @foreach (var test2 in item) 
       { 
      <tbody> 
       <tr> 
        <td> 
         @Html.DisplayFor(modelItem => test2.ExerciseName) 
        </td> 
        <td> 
         @Html.DisplayFor(modelItem => test2.ExerciseTime) 
        </td> 
        <td> 
         @Html.DisplayFor(modelItem => test2.ExerciseRepetition) 
        </td> 
        <td> 
         @Html.DisplayFor(modelItem => test2.MomentOfTheDay) 
        </td> 
        <td> 
         @Html.DisplayFor(modelItem => test2.Routine.RoutineID) 
        </td> 
        <td> 
         @Html.DisplayFor(modelItem => test2.ExerciseWeek) 
        </td> 
        <td> 
         <a asp-action="Edit" asp-route-id="@test2.ExerciseID">Edit</a> | 
         <a asp-action="Details" asp-route-id="@test2.ExerciseID">Details</a> | 
         <a asp-action="Delete" asp-route-id="@test2.ExerciseID">Delete</a> 
        </td> 
       </tr> 


      </tbody> 
      } 


    </table> 

    } 

一切工作正常,但

<h2>@Html.Display(item.Select(x => x.DayName).ToString())</h2> 

我只是想展现天名称上表因为它按天分组,但显示代码不会显示任何内容。我试过使用DisplayFor,但显然它不接受表达式。或者,也许我做错了。

回答

1

Html.Display不适用于此目的,这就是为什么它不起作用。你需要的是Html.DisplayFor。但是,您得到的错误是因为该参数必须是一个表达式,该表达式将评估为模型上的成员。使用类似Select的东西是不可能的,因为表达式不能被解析为特定的成员。

现在,鉴于您在此处使用Select,目前尚不完全清楚您期望看到什么样的显示。这将是一个枚举,所以你需要做一些关于如何处理这个枚举中的每个项目的决定。简单地说,你可以这样做:

<h2> 
    @foreach (var item in items) 
    { 
     @Html.DisplayFor(x => x.DayName) 
    } 
</h2> 

然而,由于这是一个标题,很可能你只是希望某一天的名字,所以你可能宁愿只是这样做:

@{ var item = item.First(); } 
<h2>@Html.DisplayFor(x => item.DayName)</h2> 

然而,在这里甚至需要DisplayFor并不完全清楚。如果DayName只是一个像Monday这样的字符串,那么DisplayFor是完全多余的;它只会输出字符串。因此,你可以这样做:

<h2>@items.Select(x => x.DayName).First()</h2> 
+0

我几乎有正确的做法哈哈。非常感谢你清除那个。 – LeinadDC