Helper ermöglichen die Verwendung und Rückgabe von direktem HTML:
@helper OrderedList(IEnumerable<string> items){
<ol>
@foreach(var item in items){
<li>@item</li>
}
</ol>
}
@OrderedList(new[] { "Blue", "Red", "Green" })
Bei functions kann man kein HTML verwenden:@using System.Web.Mvc;
@using System.Text;
@functions {
public static HtmlString OrderedList(IEnumerable<string> items)
{
var sb = new StringBuilder();
var orderedList = new TagBuilder("ol");
foreach(var item in items){
var listItem = new TagBuilder("li");
listItem.SetInnerText(item);
sb.AppendLine(listItem.ToString(TagRenderMode.Normal));
}
orderedList.InnerHtml = sb.ToString();
return new HtmlString(orderedList.ToString(TagRenderMode.Normal));
}
}
@OrderedList(new[] { "Blue", "Red", "Green" })
b1d6ebc3-70ab-40ec-9308-76ff96a0044d|0|.0|96d5b379-7e1d-4dac-a6ba-1e50db561b04