简体   繁体   English

如何在嵌套模型中访问 DisplayNameFor

[英]How do you access the DisplayNameFor in a nested model

How do you access the DisplayNameFor in a nested model - ie.:您如何在嵌套模型中访问DisplayNameFor - 即:

public class Invoice
{
    public int InvoiceId { get; set; }
    public string CustomerName { get; set; }
    public string Contact { get; set; }
    public IList<InvoiceItem> InvoiceItems { get; set; }
}

public class InvoiceItem
{
    public int InvoiceItemId { get; set; }
    public int InvoiceId { get; set; }
    [Display(Name = "Item Heading")]
    public string Item { get; set; }
    [Display(Name = "Item Description")]
    public string Description { get; set; }
    public virtual Invoice Invoice { get; set; }
}

I can loop through the nested items as follows:我可以按如下方式循环遍历嵌套项:

     @foreach (var item in Model.InvoiceItems)
     {
          @Html.DisplayFor(modelItem => item.Item)
          <br />
          @Html.DisplayFor(modelItem => item.Description)
          <br />
     }  

But I how do I get to: [Display(Name = "Item Heading")]但是我如何到达: [Display(Name = "Item Heading")]

@Html.DisplayNameFor(modelItem => modelItem.Item)

I get the error: 'inv5.Models.Invoice' does not contain a definition for 'Item' and no extension method 'Item' accepting a first argument of type 'inv5.Models.Invoice' could be found我收到错误消息: 'inv5.Models.Invoice' does not contain a definition for 'Item' and no extension method 'Item' accepting a first argument of type 'inv5.Models.Invoice' could be found

Thanks, Mark谢谢,马克

Here you're accessing the item in your loop:在这里,您正在访问循环中的item

@Html.DisplayFor(modelItem => item.Item)

But here you're not:但在这里你不是:

@Html.DisplayNameFor(modelItem => modelItem.Item)

Change the latter to this and it should work:将后者更改为此,它应该可以工作:

@Html.DisplayNameFor(modelItem => item.Item)

modelItem is of type Invoice (the model passed to the view), but in the loop you want to use item (from the loop iterator) which is of type InvoiceItem . modelItemInvoice类型(传递给视图的模型),但在循环中您要使用InvoiceItem类型的item (来自循环迭代器)。

Side note: Debugging something like this might be a lot more straightforward if everything wasn't called "item."附注:如果一切都没有叫调试这样的事情可能有很多更直接的“项目”。 Clear naming is important :)清晰的命名很重要:)

Edit: If you're accessing it outside of the loop, then you need to drill down into the model manually.编辑:如果您在循环之外访问它,那么您需要手动深入到模型中。 Something like this:像这样的东西:

@Html.DisplayNameFor(modelItem => modelItem.InvoiceItems.First().Item)

It feels like this would be dangerous, since .First() can throw an exception if there are no items.感觉这会很危险,因为如果没有项目, .First()可能会抛出异常。 However, as comments have indicated and some cursory Googling seems to confirm, .First() isn't actually executed at runtime in this case.然而,正如评论所指出的和一些粗略的谷歌搜索似乎证实,在这种情况下, .First()实际上并没有在运行时执行。 This is just a bit of trickery to allow .DisplayNameFor() to reflect down into the property.这只是让.DisplayNameFor()反映到属性中的一些.DisplayNameFor()

You can create a DisplayTemplate for your InvoiceItems , say "InvoiceItemHeader":您可以为您的InvoiceItems创建一个 DisplayTemplate ,比如“InvoiceItemHeader”:

@model IEnumerable<InvoiceItem>

@Html.DisplayNameFor(m => m.Item)
<br />
@Html.DisplayNameFor(m => m.Description)

The type of the model has to be IEnumerable<T> , as the DisplayNameFor Method has an overload version for this type.模型的类型必须是IEnumerable<T> ,因为DisplayNameFor 方法具有此类型的重载版本。

Then in your view you can display the template:然后在您的视图中您可以显示模板:

 @Html.DisplayFor(m => m.InvoiceItems, "InvoiceItemHeader")
 @foreach (var item in Model.InvoiceItems)
 {
      @Html.DisplayFor(modelItem => item.Item)
      <br />
      @Html.DisplayFor(modelItem => item.Description)
      <br />
 } 

I have not tried this, but it should work.我没有试过这个,但它应该工作。

@Html.DisplayNameFor(modelItem => modelItem.First().InvoiceItems().First().Item)

DisplayNameFor extension method is nothing else than a method expecting a Memberexpression with this signature: Expression<Func<TModel, TValue>> expression DisplayNameFor 扩展方法只不过是一个需要具有以下签名的 Memberexpression 的方法: Expression<Func<TModel, TValue>> expression

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

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