這是最新的MVC 5.2HTML輔助工作在ASPX但不是剃刀
這裏是代碼從一個被遺忘的博客使用多年的助手前
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;
if (items.Count() == 0)
return;
var i = 0;
foreach (var item in items)
{
if (i != 0 && i % itemsBeforeSplit == 0)
seriesSplitter();
template(item);
i++;
}
}
我使用這樣的
<table class="public-photo" >
<tr class="public-photo">
<% Html.SeriesSplitter(Model.Photos, 6, item => { %>
<td class="public-photo">
<a href="/Anon/DisplayPublicPhoto/?filename=<%=item.PublicFileName%>" class="public-photo" >
<%if(Model.HighRes == true){%>
<img src="/t2/<%=item.PublicFileName%>" id="<%=item.PublicFileName%>" class="public-photo" alt="thumbnail" width="140" height="105" />
<%}
else
{%>
<img src="/t/<%=item.PublicFileName%>" id="<%=item.PublicFileName%>" class="public-photo" alt="thumbnail" width="140" height="105" />
<% } %>
</a>
<a href="#" class="ReportPhoto" id="<%=item.PhotoID%>">Report this</a>
</td>
<%},() => { %>
</tr>
<tr>
<% }); %>
</tr>
</table>
在aspx中沒有問題,但剃刀似乎在動作參數的最後部分窒息,其中html代碼與代碼交互。對於剃刀我把它轉化爲正確的語法 編輯剃刀代碼
<table class="public-photo">
<tr class="public-photo">
@Html.SeriesSplitter(Model.Photos, 6, icetem =>
{
@<td class="public-photo">
<a href="/Anon/DisplayPublicPhoto/[email protected] " class="public-photo">
@if (Model.HighRes == true)
{
<img src="/t2/@icetem.PublicFileName" id="@icetem.PublicFileName" class="public-photo" alt="thumbnail" width="140" height="105" />
}
else
{
<img src="/t/@icetem.PublicFileName" id="@icetem.PublicFileName" class="public-photo" alt="thumbnail" width="140" height="105" />
}
</a>
<a href="#" class="ReportPhoto" id="@icetem.PhotoID">Report this</a>
</td>;
},
() => {
//</tr>
// <tr>
});
</tr>
</table>
錯誤是CS0201:只有分配,調用,遞增,遞減,在等待着,新對象表達式可以用作聲明
是否有錯誤訊息?這似乎很重要。否則,結果如何?看起來你只給了我們你的Web窗體代碼。 Razor代碼在哪裏? – mason 2014-11-05 20:08:45
堅持在這裏找到一個類似的問題http://stackoverflow.com/questions/4883034/htmlhelper-in-asp-net-mvc3-that-uses-actiont-as-template-razor-syntax – Joe 2014-11-05 20:34:57
@Joe哪一行是你的錯誤發生在? – mason 2014-11-05 20:38:38