SomeViewModel 看起来像这样 class SomeViewModel { public IEnumerable Foos {get;-6ren">
gpt4 book ai didi

c# - foreach 中的 LabelFor()

转载 作者:IT王子 更新时间:2023-10-29 04:33:21 26 4
gpt4 key购买 nike

我有一个关联了强类型模型的 View

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<SomeNamespace.SomeViewModel>" %>

SomeViewModel 看起来像这样

class SomeViewModel
{
public IEnumerable<Foo> Foos {get; set;}
}

然后说 Foo 是

class Foo
{
public string Bar {get; set;}
}

在 View 中

<% foreach (var item in Model.Foos) { %>
<tr>
<td>
<%= Html.LabelFor(f => f.Bar) %>
</td>

我不确定如何使用 Html.LabelFor()item 中显示 Bar 属性

有人可以帮我解决这个问题吗?

谢谢,

最佳答案

改为这样做:

<% foreach (var item in Model.Foos) { %>      
<tr>
<td>
<%= Html.LabelFor(f => item.Bar) %>
</td>
<% } %>

将 f => f.Bar 改为 f => item.Bar,其中 item 是 foreach 循环中变量的名称。

这是更漂亮的 Razor 语法:)

@foreach( var item in Model.Foos ) {
<tr>
<td>
@Html.LabelFor(f => item.Bar)
</td>
<td>
@Html.DisplayFor(f => item.Bar)
</td>
</tr>
}

关于c# - foreach 中的 LabelFor(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7445432/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com