代码之家  ›  专栏  ›  技术社区  ›  devuxer

处理ASP.NET MVC“标签汤”

  •  4
  • devuxer  · 技术社区  · 15 年前

    我今天正在开发一个ASP.NET MVC模板,在盯着那些荧光黄的模板之后 % 标签足够长的时间,我基本上决定我已经有足够的,所以我刻意修改了我的ascx文件,如下所示:

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl"         %>
    
    <%  if (Model == null)
        {                                                                       %>
    <%=     ViewData.ModelMetadata.NullDisplayText                              %>
    <%  }
        else if (ViewData.TemplateInfo.TemplateDepth > 1)
        {                                                                       %>
    <%=     ViewData.ModelMetadata.SimpleDisplayText                            %>
    <%  }
        else
        {                                                                       %>
    <%      foreach (var prop in ViewData.ModelMetadata.Properties.Where(
                pm => pm.ShowForDisplay && !ViewData.TemplateInfo.Visited(pm)))
            {                                                                   %>
    <%          if (prop.HideSurroundingHtml)
                {                                                               %>
    <%=             Html.Display(prop.PropertyName)                             %>
    <%          }
                else
                {                                                               %>
    <%              if (!String.IsNullOrEmpty(prop.GetDisplayName()))
                    {                                                           %>
                        <span class="display-label">
    <%=                     prop.GetDisplayName()                               %>
                        </span>
    <%              }                                                           %>
                    <span class="display-field">
    <%=                 Html.Display(prop.PropertyName)                         %>
                    </span>
    <%          }                                                               %>
    <%      }                                                                   %>
    <%  }                                                                       %>
    

    最后是可读性。唯一的问题是,这需要 方式 渴望用手来做这件事。我需要一种自动化的方法。某种代码格式解决方案。可能是宏或Visual Studio加载项,还是…?你有什么建议?

    更新

    我现在计划从我的标记中重构出大部分逻辑(见下面迈克的回答),但与此同时,我想出了一种更易于管理的方法来格式化代码和HTML混合的ascx文件。代码在这种方式下的分布稍微大一点,但是首先这样格式化代码要容易得多,而且使用起来也要容易得多。

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
    
    <% 
        if (Model == null)
        {
    %>
            <%= ViewData.ModelMetadata.NullDisplayText %>
    <%
        }
        else if (ViewData.TemplateInfo.TemplateDepth > 1)
        {
    %>
            <%= ViewData.ModelMetadata.SimpleDisplayText %>
    <%
        }
        else
        {
    %>
    <%
            foreach (var prop in ViewData.ModelMetadata.Properties.Where(
                pm => pm.ShowForDisplay && !ViewData.TemplateInfo.Visited(pm)))
            {
                if (prop.HideSurroundingHtml)
                {
    %>
                    <%= Html.Display(prop.PropertyName) %>
    <%
                }
                else
                {
    %>
                    <div class="display-row">   
    <%
                    if (!String.IsNullOrEmpty(prop.GetDisplayName()))
                        {
    %>
                            <div class="display-label">
                                <%= prop.GetDisplayName() %>
                            </div>
    <%
                    }
    %>
                        <div class="display-field">
                            <%= Html.Display(prop.PropertyName) %>
                        </div>
                </div>
    <%
                }
            }
        }
    %>
    
    3 回复  |  直到 15 年前
        1
  •  16
  •   Mike Scott    15 年前
        2
  •  3
  •   Arnis Lapsa    15 年前


    <var nullText="ViewData.ModelMetadata.NullDisplayText"
         templateDepth="ViewData.TemplateInfo.TemplateDepth"
         simpleDisplayText="ViewData.ModelMetadata.SimpleDisplayText"
         props="ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForDisplay && !ViewData.TemplateInfo.Visited(pm))" 
         m="ViewData.Model"/>
    <if condition="m==null">
      ${nullText}
    </if>
    <elseif condition="templateDepth>1">
      ${simpleDisplayText}
    </elseif>
    <else>
      <for each="var prop in props">
        <if condition="prop.HideSurroundingHtml">
          ${Html.Display(prop.PropertyName)}
        </if>
        <else>
          <span if="!string.IsNullOrEmpty(prop.GetDisplayName()" class="display-field">
            ${prop.GetDisplayName()}
          </span>
          <span class="display-field">
            ${Html.Display(prop.ProperyName)}
          </span>
        </else>
      </for>
    </else>
    

        3
  •  0
  •   The Matt    15 年前

    public static void WriteIf(this HtmlHelper helper, bool condition, string truePart, string falsePart)
    {
         helper.ViewContext.HttpContext.Response.Write(condition ? truePart : falsePart);
    }
    

    <% Html.WriteIf(Model.IsTrue(), "TrueText", "FalseText"); %>