名称“ modelItem”在当前上下文中不存在(在foreach循环内)

时间:2019-01-23 17:58:11

标签: database foreach .net-core razor-pages tag-helpers

我在这里有一个.NET Core 2.0应用程序,在我的一种观点中,我使用了foreach循环来显示此数据库表中的所有项目。一切都很好。现在,我在Bootstrap模态中包含的另一个foreach循环中显示了ID列表,但是现在我看到了一条错误消息

  

名称“ modelItem”在当前上下文中不存在

我确定原因。看起来一切正常。有人可以指出为什么我遇到此错误,或者提供一种替代方法从数据库中提取所需信息以这种方式显示吗?

查看

@foreach (var Submission in Model.Submissions)
{
    <div class="col-sm-3">
        // Code that populates the project cards on the page
    </div>

    <!-- Modal -->
    <div class="modal fade" id="StatusCodeUpdate-@Submission.ID" tabindex="-1" role="dialog" aria-labelledby="StatusCodeUpdateLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
        <div class="modal-header">
            <h5 class="modal-title" id="StatusCodeUpdateLabel">Update Status Code for @Submission.Title </h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
            </button>
        </div>
        <div class="modal-body">
            <h6>Status Codes</h6>
            <form method="post">
                @foreach (var Code in Model.StatusCodes)
                {
                    @Html.DisplayFor(modelItem = Code.ID) // ERROR!!!

                }
            </form>
        </div>
        ...
}

隐藏代码

public class TeamleadModel : PageModel
{
    public IList<Submission> Submissions { get; set; }
    public IList<Status> StatusCodes { get; set; }
    private readonly SubmissionContext _submissionContext;
    private readonly StatusContext _statusContext;
    public TeamleadModel(SubmissionContext submission, StatusContext statusContext)
    {
        _submissionContext = submission;
        _statusContext = statusContext;
    }

    public async Task OnGetAsync()
    {
        Submissions = await _submissionContext.Submissions.ToListAsync();
        StatusCodes = await _statusContext.StatusCodes.ToListAsync();
     }

1 个答案:

答案 0 :(得分:1)

尝试@Html.DisplayFor(modelItem => Code.ID)。您有一个分配而不是lambda表达式。