2010-10-30 70 views
1

所以,我把所有这些逻辑放在我的视角,认为遵循DRY原则会有所帮助。ASP.NET MVC2 DRY视图模式?

基本上,这种复制一个文件的形式,并在我的视图模型,我对命名为喜欢XXXConditionXXXComment其放在一起形成我的实际模型的InspectionDetail性能。

<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> 

视图中的所有代码看起来像是一个确定的反模式。会有更好的地方来隐藏这个吗?或者是愚蠢的依靠订单属性定义在视图模型在所有

回答