Spitting out HTML with asp.net mvc
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.
精彩评论