Does mvc3 razor engine supports output from List.ForEach()
I have 2 code block, first:
@foreach(var filterName in Model.FilterNames){
<text>
$("#@filterName").combobox({
source:"@(filterName)Autocomplete"
});
</text>
}
second:
@Model.FilterNames.ForEach(filterName => {
<text>
$("#@filte开发者_高级运维rName").combobox({
source:"@(filterName)Autocomplete"
});
</text>
})
Second one doesn't work. Anyone knows if razor supports this syntax? Or what i'm doing wrong?
When using a method in razor, it will assume that it is an helper or a property and return either an HelperResult or data.
As the List.ForEach method does return nothing, the call will fail.
ForEach
is an method on List<T>
and a usually an extension method people create on IEnumerable<T>
(so your probably missing the @using
directive for System.Collections.Generic
and/or the extension method altogether), but it's considered by many (including Eric Lippert) to be poor design from a functional programming perspective.
Stick with your first one.
Or better yet, use an Editor Template and save the loop altogether.
精彩评论