当模型是 IEnumerable<T> 时,如何在 asp-for 标签助手中使用本地化
How to use localization in asp-for tag helper, when the model is an IEnumerable<T>
在我看来,该模型来自 IEnumerable<ApplicationUser>
类型。在此模型中使用 asp-for
标签参数的最佳做法是什么?
我的意思是:当模型来自类型 ApplicationUser
并且我们创建一个简单的 "Edit-All-Data"-模型时,我们可以简单地做这样的事情:
<label asp-for="Model.FirstName"></label>
<input asp-for="Model.FirstName"></input>
<label asp-for="Model.LastName"></label>
<input asp-for="Model.LastName"></input>
但现在我的模型是 IEnumerable<ApplicationUser>
(我想利用资源本地化和 ApplicationUser
的 DisplayAttribute
)并且我想写一个 table:
<table>
<tr>
<th><label asp-for="??? FirstName ???"></label></th>
<th><label asp-for="??? LastName ???"></label></th>
<th> </th>
</tr>
@foreach (var user in Model)
{
<tr>
<td>@user.FirstName</td>
<td>@user.LastName</td>
<td><a asp-action="EditUser" asp-controller="Administrator" asp-route-id="@user.Id">edit profile</a></td>
</tr>
}
</table>
如何在 <th>...</th>
标签中使用 asp-for
?我错过了什么吗?
所以要在这里提供答案,我们可以使用 IEnumerable<T>
的扩展方法。以上来自@jmcilhinney 和@Tseng 的评论帮助很大
<table>
<tr>
<th><label asp-for="First().FirstName"></label></th>
<th><label asp-for="First().LastName"></label></th>
<th> </th>
</tr>
....
</table>
我将用 if (Model != null && Model.Any())
包围它以确保它既不会抛出 NullReferenceException
也不会抛出 IndexOutOfRangeException
。
在我看来,该模型来自 IEnumerable<ApplicationUser>
类型。在此模型中使用 asp-for
标签参数的最佳做法是什么?
我的意思是:当模型来自类型 ApplicationUser
并且我们创建一个简单的 "Edit-All-Data"-模型时,我们可以简单地做这样的事情:
<label asp-for="Model.FirstName"></label>
<input asp-for="Model.FirstName"></input>
<label asp-for="Model.LastName"></label>
<input asp-for="Model.LastName"></input>
但现在我的模型是 IEnumerable<ApplicationUser>
(我想利用资源本地化和 ApplicationUser
的 DisplayAttribute
)并且我想写一个 table:
<table>
<tr>
<th><label asp-for="??? FirstName ???"></label></th>
<th><label asp-for="??? LastName ???"></label></th>
<th> </th>
</tr>
@foreach (var user in Model)
{
<tr>
<td>@user.FirstName</td>
<td>@user.LastName</td>
<td><a asp-action="EditUser" asp-controller="Administrator" asp-route-id="@user.Id">edit profile</a></td>
</tr>
}
</table>
如何在 <th>...</th>
标签中使用 asp-for
?我错过了什么吗?
所以要在这里提供答案,我们可以使用 IEnumerable<T>
的扩展方法。以上来自@jmcilhinney 和@Tseng 的评论帮助很大
<table>
<tr>
<th><label asp-for="First().FirstName"></label></th>
<th><label asp-for="First().LastName"></label></th>
<th> </th>
</tr>
....
</table>
我将用 if (Model != null && Model.Any())
包围它以确保它既不会抛出 NullReferenceException
也不会抛出 IndexOutOfRangeException
。