开发者

ASP.NET MVC2 DRY view pattern?

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()) {
           ob开发者_StackOverflow中文版ject[] 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜