gpt4 book ai didi

c# - 对列表项使用标签助手验证

转载 作者:行者123 更新时间:2023-11-30 23:10:17 24 4
gpt4 key购买 nike

我看过 this question ,这似乎完全涵盖了我想做的事情,但出于某种原因,那里提供的解决方案对我不起作用。

我的观点如下:

@model ExerciseEditModel

@foreach (var item in Model.Rounds)
{
<!-- bootstrap grid markup omitted -->
<div class="form-group col-sm-3">
<label asp-for="item.DurationEstimate"></label>
<input asp-for="item.DurationEstimate" type="time" class="form-control" />
</div>
<!-- bootstrap grid markup, and more form elements, omitted -->
}

据我所知,这与链接问题中的解决方案的工作方式完全一致,但我在 View 上遇到构建错误,因为

'ExerciseEditModel' does not contain a definition for 'item' and no extension method 'item' accepting a first argument of type 'ExerciseEditModel' could be found (are you missing a using directive or an assembly reference?)

似乎 asp-for 帮助程序的范围没有注意到 foreach 循环 - 我怎样才能让它工作?

最佳答案

来自docs :

The asp-for attribute value is a ModelExpression and the right hand side of a lambda expression. Therefore, asp-for="Property1" becomes m => m.Property1 in the generated code which is why you don't need to prefix with Model. You can use the "@" character to start an inline expression and move before the m.

所以你应该在你的表达式前加上一个@:

@model ExerciseEditModel

@foreach (var item in Model.Rounds)
{
<!-- bootstrap grid markup omitted -->
<div class="form-group col-sm-3">
<label asp-for="@item.DurationEstimate"></label>
<input asp-for="@item.DurationEstimate" type="time" class="form-control" />
</div>
<!-- bootstrap grid markup, and more form elements, omitted -->
}

关于c# - 对列表项使用标签助手验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45439872/

24 4 0