Question

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

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top