@{ int counter=0; }
@foreach(var item in Model.items){
counter++;
<span>@item.Name</span>
if(counter<Model.Items.Count)
{
@Html.Raw("|")
}
}
과 짧은 버전입니다.
당신은 더 복잡 마크 업에 대한 면도기 @helper
방법이 혼합 할 수 있습니다
public static HtmlString LoopWithSeparator
(this HtmlHelper helper, string separator, IEnumerable<object> items)
{
return new HtmlString
(helper.Raw(string.Join(separator, items)).ToHtmlString());
}
사용법 :
이
@helper ComplexMarkup(ItemType item)
{
<span>@item.Name</span>
}
@Html.Raw(string.Join("|", model.Items.Select(s => ComplexMarkup(s))))
당신은 심지어 추상적으로 도우미 방법 Html.Raw()
및 string.Join()
전화를 만들 수 있습니다
@Html.LoopWithSeparator("|", model.Items.Select(s => ComplexMarkup(s)))
+1 다른 것보다 더 우아함 ... –
이것은 매우 간단한 루프 바디에만 적합합니다. – Marius
참, 앞면 개선을 위해 편집 참조 ... – Oliver