当前上下文中不存在名称 'modelItem'(在 foreach 循环内)
The name 'modelItem' does not exist in the current context (inside foreach loop)
我这里有一个 .NET Core 2.0 应用程序,在我的一个观点中,我使用 foreach
循环来显示此数据库 table 中的所有项目。一切都很好。现在我在 Bootstrap 模态中包含的那个循环中添加了另一个 foreach
循环以显示 ID 列表,但现在我收到一个错误
The name 'modelItem' does not exist in the current context
我确定原因。看起来一切正常。有人可以指出为什么我会收到此错误或提供另一种方法来从数据库中提取所需信息以显示在此模式中吗?
查看
@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">×</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();
}
尝试@Html.DisplayFor(modelItem => Code.ID)
。你有一个赋值而不是 lambda 表达式。
我这里有一个 .NET Core 2.0 应用程序,在我的一个观点中,我使用 foreach
循环来显示此数据库 table 中的所有项目。一切都很好。现在我在 Bootstrap 模态中包含的那个循环中添加了另一个 foreach
循环以显示 ID 列表,但现在我收到一个错误
The name 'modelItem' does not exist in the current context
我确定原因。看起来一切正常。有人可以指出为什么我会收到此错误或提供另一种方法来从数据库中提取所需信息以显示在此模式中吗?
查看
@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">×</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();
}
尝试@Html.DisplayFor(modelItem => Code.ID)
。你有一个赋值而不是 lambda 表达式。