我已经创建了一个简单的Gallery html helper,目前它不够漂亮,因为我无法使用Razor。 (我也喜欢用行动为我会与web表单视图引擎完成)MVC Razor如何使用我的HTML助手?
public static HelperResult Gallery<T>(this HtmlHelper html,
IEnumerable<T> items,
int itemsPerRow,
Action rowContainer,
Func<T, HelperResult> itemContainer,
Action endRowContainer)
{
if (items == null)
return new HelperResult(writer => { });
int itemCount = 1;
return new HelperResult(writer =>
{
rowContainer();
foreach (var item in items)
{
if (itemCount % itemsPerRow == 0)
{
endRowContainer();
rowContainer();
}
itemContainer(item).WriteTo(writer);
itemCount++;
}
endRowContainer();
});
}
这是intened使用,但rowContainer和endRowContainer在生成的HTML的顶部outputed
@Html.Gallery(
Model.Stores,
3,
()=> { Response.Write("<div class=\"portfolio_box_container\">"); },
@<div class="portfolio_box" style="padding-right: 25px">
<img src="item.png" width="120" height="43" alt="" />
</div>
,
() => { Response.Write("</div>"); })
我在做什么错了?
如果要打印出来是这样的:
<div class="portfolio_box_container">
<div class="portfolio_box" style="padding-right: 25px">
<img src="item.png" width="120" height="43" alt="" />
</div>
<div class="portfolio_box" style="padding-right: 25px">
<img src="item.png" width="120" height="43" alt="" />
</div>
<div class="portfolio_box" style="padding-right: 25px">
<img src="item.png" width="120" height="43" alt="" />
</div>
</div>
<div class="portfolio_box_container">
<div class="portfolio_box" style="padding-right: 25px">
<img src="item.png" width="120" height="43" alt="" />
</div>
<div class="portfolio_box" style="padding-right: 25px">
<img src="item.png" width="120" height="43" alt="" />
</div>
<div class="portfolio_box" style="padding-right: 25px">
<img src="item.png" width="120" height="43" alt="" />
</div>
</div>
它是什么现在印刷这样的动作参数?或者它抛出一个错误? – 2011-05-30 02:07:21