views:

52

answers:

1

So, I put all this logic in my view, thinking it would be helpful to follow the DRY principle.

Basically, this replicates a paper form, and in my view model, I have pairs of properties named like XXXCondition and XXXComment which put together form an InspectionDetail in my actual model.

   <table>
    <% string secname ="";
       foreach (PropertyInfo p in typeof(InspectionFormModel).GetProperties()) {
           object[] sec = p.GetCustomAttributes(typeof(InspectionSection), false);
           object[] name = p.GetCustomAttributes(typeof(DisplayNameAttribute), false);
           string propname = p.Name;

           /* Display a row for all view model properties having InspectionSection Attribute */
           if ( sec.Length > 0) {

               /* New Subheading */
               if (((InspectionSection)sec[0]).Section.ToString() != secname) {
                   secname = ((InspectionSection)sec[0]).Section.ToString();
                   var secdesc = typeof(InspectionDetail.DetailTypes).GetMember(secname)[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
                   string ssecdesc = (secdesc.Length == 0) ? secname : ((DescriptionAttribute)secdesc[0]).Description;
                   %> <tr><th><%= ssecdesc %></th><th>Condition</th><th>Remarks</th></tr><%
               }

               /* Use DisplayName attribute for item instead of property name if specified */
               if (name.Length > 0)
               {
                   propname = ((DisplayNameAttribute)name[0]).DisplayName;
               }%>
    <tr>
        <td><label for="<%= p.Name %>"><%= propname %></label></td>
        <td><%= Html.DropDownList(p.Name, (IEnumerable<SelectListItem>)ViewData["ItemConditions"]) %></td>
        <td><%= Html.TextBox(p.Name.Replace("Condition", "Comment"), null, new {Class ="comment"}) %></td>
    </tr>
    <% } } %>
    </table>

All this code in the view seems like a definite anti-pattern. Would there be a better place to hide this? Or is it foolish to rely on the order properties are defined in a view model at all?

+1  A: 

Have you looked at things like MVC templates: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html?

You should probably also create a custom view model and initialize it in your controller/action method from the various attributes etc.

marcind
That advice should solve all of the issues I have with this code. Thanks.
mootinator