문제

Here is a way that we are currently spitting out some HTML to our view.

            foreach (var reportGroup in managedReportGroup.CustomerTags)
            { %>

                <%= reportGroup.Name %>

                <%
            }

Is there something that I can do like this? (This won't work for me)

        Action<ManagedReportGroups> handlesReportGroup = delegate(ManagedReportGroups managedReportGroup)
        {
            foreach (var reportGroup in managedReportGroup.CustomerTags)
            {
                Html.Encode(reportGroup.Name);                    
            }
        };  

The reason would be to cut down on the aligator brackets and clean up my code.

Thanks,

Mike

도움이 되었습니까?

해결책

If you move it to partial views, you will still have to do the loop inside the partial.

You could create custom Html extension methods if you were really dead-set against your first example.

Or if your main goal really is to reduce "alligator brackets" you could cut it down to two if you did it this way:

<%
    foreach (var reportGroup in managedReportGroup.CustomerTags)
    {
        Response.Write(reportGroup.Name)
    }
%>

You might also consider the Spark View Engine

다른 팁

Why not use PartialViews?

foreach (var reportGroup in managedReportGroup.CustomerTags)
            { %>

                <% Html.RenderPartial("NamePartialView", reportGroup.Name) %>

            }

Then your partial view can render the name.

This is more effective when you pass in the reportGroup to the PartialView and let the partialView then either write all the HTML or call further PartialViews each rendering at a more atomic level.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top