简体   繁体   English

在foreach中的LabelFor()

[英]LabelFor() in a foreach

I have a view with a strongly-typed model associated with it 我有一个与它相关的强类型模型的视图

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

The SomeViewModel looks like this SomeViewModel看起来像这样

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

and say Foo is 并说Foo是

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

and in the view 并在视图中

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

I'm not sure how to display Bar property in item using Html.LabelFor() 我不知道如何使用Html.LabelFor()item显示Bar属性

Can someone help me with this? 有人可以帮我弄这个吗?

Thanks, 谢谢,

Do this instead: 改为:

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

Instead of f => f.Bar do f => item.Bar, where item is the name of the variable in your foreach loop. 而不是f => f.Bar do f => item.Bar,其中item是foreach循环中变量的名称。

Here is the much more beautiful razor syntax :) 这是更美丽的剃刀语法:)

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

have you tried 你有没有尝试过

<% for (var i=0; i<Model.Foos.Count();i++) { %> 
    <tr> 
        <td> 
            <%= Html.LabelFor(f => f.Foos[i].Bar) %> 
        </td> 

The LabelFor extension uses a lamda expression to go from your Model object to the selected property, consider fa substituion variable for your model. LabelFor扩展使用lamda表达式从Model对象转到selected属性,考虑模型的fa替换变量。 So you need a way to get from your model to your chosen property. 因此,您需要一种方法从您的模型到您选择的房产。 You are telling LabelFor, create a local variable called f and assign it with the value of your model. 您正在告诉LabelFor,创建一个名为f的局部变量,并为其指定模型的值。 then use the bit after the => to determine the target property. 然后使用=>之后的位来确定目标属性。

If you are desperate to use the foreach, you would have to have a way of translating item back to a property of the original model, (eg for an array as Gledrius said x=>x.Foos[Model.Foos.IndexOf(foo)]) 如果你急于使用foreach,你必须有一种方法将项目转换回原始模型的属性,(例如对于数组,如Gledrius所说x=>x.Foos[Model.Foos.IndexOf(foo)])

failing that if you just want the text value 如果您只想要文本值,那就失败了

use '<%= item %>' instead of the whole labelfor or if you have ASP.NET 4 and MVC2 or better use <%: item %> for that HTML encoding goodness 使用'<%= item%>'而不是整个标签,或者如果你有ASP.NET 4和MVC2或更好地使用<%: item %>来获得HTML编码的优点

You could also just write a custom extension that's not tied directly to the Model of the page. 您也可以编写一个不直接与页面模型绑定的自定义扩展。

   public static MvcHtmlString LabelForItem<T, V>(this HtmlHelper html, T obj, Expression<Func<T, V>> expression) 
   {
       Func<T,V> func = expression.Compile();
       V val = func(obj);
       return html.Label(val.ToString());
   }

And you'd use it, like this: 而且你会使用它,像这样:

@foreach (Foo foo in Model)
{
    <p>
       @(Html.LabelForItem<Foo, string>(foo, f => f.Name))
    </p>
}

From here , looks like you need to specify the DisplayName on the property: 这里看,您需要在属性上指定DisplayName

[DisplayName("My Bar")]
public string Bar { get; set; }

如果Foos是列表,它将如下所示:

Html.LabelFor(x=>x.Foos[Model.Foos.IndexOf(item)])

你正在迭代你的Foo,但根本没有使用item变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM