【问题标题】:Filtering in MVC Razor using LINQ使用 LINQ 在 MVC Razor 中进行过滤
【发布时间】:2013-11-16 03:27:32
【问题描述】:

在我看来,我有一个模型StudentListModel,如下:

public class StudentListModel
{
    public IEnumerable<StudentModel> SModel { get; set; }
    public int StudentId { get; set; }
}

在我看来,我想过滤 SModel 并将其绑定到文本框。我正在使用以下代码:

@Html.DisplayFor(score => score.SModel.Where(x=>x.StudentId==1))

但我收到以下错误:

模板只能用于字段访问、属性访问、 一维数组索引,或单参数自定义索引器 表达式。

哪种过滤方式最好?

【问题讨论】:

    标签: linq razor


    【解决方案1】:

    您不能直接这样过滤,因为@html.textboxfor 无法显示列表。

    您可以使用以下代码:

    @foreach (var item in @Model.SModel.Where(x=>x.StudentId==1))
    {      
        <li>@Html.DisplayFor(score => item.StudentName)</li>
    }  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多