【问题标题】:Passing HTML Elements in as Func<> in Razor View Engine在 Razor 视图引擎中将 HTML 元素作为 Func<> 传递
【发布时间】:2012-10-15 02:30:24
【问题描述】:

我在查看一些示例代码时看到了这样的内容:

@(Html.DataList<ProductOverviewModel>(Model, 4,
      @<div class="item-box">
        <div class="item">
          <div class="picture">
            <a href="@Url.RouteUrl("Product", new { productId = item.Id, SeName = item.SeName })" title="@item.DefaultPictureModel.Title">
              <img alt="@item.DefaultPictureModel.AlternateText" src="@item.DefaultPictureModel.ImageUrl" title="@item.DefaultPictureModel.Title" /></a>
          </div>
          <div class="product-title">
            <a href="@Url.RouteUrl("Product", new { productId = item.Id, SeName = item.SeName })" title="@item.DefaultPictureModel.Title">
              @item.Name</a>
          </div>
        </div>
      </div>
))

所以基本上有一个DataList 辅助方法,其签名是这样的:

public static IHtmlString DataList<T>(this HtmlHelper helper, IEnumerable<T> items, int columns, Func<T, HelperResult> template) where T : class

他们将以下部分传递为Func&lt;T,HelperResult&gt; template,并且效果很好:

  @<div class="item-box">
    <div class="item">
      <div class="picture">
        <a href="@Url.RouteUrl("Product", new { productId = item.Id, SeName = item.SeName })" title="@item.DefaultPictureModel.Title">
          <img alt="@item.DefaultPictureModel.AlternateText" src="@item.DefaultPictureModel.ImageUrl" title="@item.DefaultPictureModel.Title" /></a>
      </div>
      <div class="product-title">
        <a href="@Url.RouteUrl("Product", new { productId = item.Id, SeName = item.SeName })" title="@item.DefaultPictureModel.Title">
          @item.Name</a>
      </div>
    </div>
  </div>

这是什么规则,怎么可能?

【问题讨论】:

    标签: c# .net asp.net-mvc asp.net-mvc-3 razor


    【解决方案1】:

    你已经把所有的词都写在里面了 :) 你正在看的是一个“模板化 Razor 代表”。我可以解释一下,但 Phil Haack 在这里做得更好:http://haacked.com/archive/2011/02/27/templated-razor-delegates.aspx

    【讨论】:

    • 看来我找到了我要找的东西。非常感谢先生!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多