在的HtmlHelper使用动作&LT ASP.NET MVC3; T>作为模板:剃刀语法? [英] HtmlHelper in ASP.NET MVC3 that uses Action<T> as template: Razor syntax?

查看:110
本文介绍了在的HtmlHelper使用动作&LT ASP.NET MVC3; T>作为模板:剃刀语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我preface此说,也许有更好的方式来做到这一点,剃刀照明方式。在任何情况下,我有一个HTML帮手,充当各种各样的中继器,但重复任意数量之后,它插入一个备用模板。最明显的用途?使得x单元格后开始一个新行的表。助手看起来是这样的:

Let me preface this by saying that perhaps there's a better way to do this, and Razor is lighting the way. In any case, I have an HTML helper that acts as a repeater of sorts, but after an arbitrary number of repeats, it inserts an alternate template. Most obvious use? Tables that start a new row after x cells. The helper looks like this:

public static void SeriesSplitter<T>(this System.Web.Mvc.HtmlHelper htmlHelper, IEnumerable<T> items, int itemsBeforeSplit, Action<T> template, Action seriesSplitter)
{
    if (items == null)
        return;
    var i = 0;
    foreach (var item in items)
    {
        if (i != 0 && i % itemsBeforeSplit == 0)
            seriesSplitter();
        template(item);
        i++;
    }
}

和在Web表单视图,用法如下:

And in a Webforms view, the usage looks like this:

<table>
    <tr>
    <% Html.SeriesSplitter(Model.Photos, 4, photo => { %>
            <td><img src="<%=ResolveUrl("~/Thumbnail.ashx?id=" + photo.ID)%>" alt="<%=Html.Encode(photo.Title)%>" /></td>
    <% }, () => { %></tr><tr><% }); %>
    </tr>
</table>

在这种情况下,你必须呈现四个单元的表,然后使用备用模板(行开始和结束标记)开始一个新行。问题是,我不能找到一种方法,使在剃刀这项工作。用剃刀视图内的lambda看起来像一个pretty怪异的结构。

In this case, you'd have a table that renders four cells, then starts a new row by using the alternate template (the row start and end tags). The problem is that I can't find a way to make this work in Razor. Using a lambda inside of a Razor view seems like a pretty weird construct.

你会怎么做?

推荐答案

其实菲尔有一个很好的职位,在这里解决了这个问题:

Phil actually had a good post that solves the problem here:

http://haacked.com/archive/2011/ 2月27日/模板 - 剃刀delegates.aspx

这篇关于在的HtmlHelper使用动作&LT ASP.NET MVC3; T&GT;作为模板:剃刀语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆