在ASP.NET MVC&QUOT处理;标签汤" [英] Dealing with ASP.NET MVC "tag soup"

查看:189
本文介绍了在ASP.NET MVC&QUOT处理;标签汤"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个ASP.NET MVC模板的今天,在那些荧光黄足够长的时间,我基本上决定,我已经受够了标签后盯着,所以我刻意修饰我的ascx文件看起来是这样的:

 <%@控制语言=C#继承=System.Web.Mvc.ViewUserControl%GT;<如果%(型号== NULL)
    {%GT;
&所述;%= ViewData.ModelMetadata.NullDisplayText%GT;
<%}
    否则如果(ViewData.TemplateInfo.TemplateDepth→1)
    {%GT;
&所述;%= ViewData.ModelMetadata.SimpleDisplayText%GT;
<%}
    其他
    {%GT;
<%的foreach(在ViewData.ModelMetadata.Properties.Where VAR道具(
            PM => pm.ShowForDisplay&功放;&安培; !ViewData.TemplateInfo.Visited(PM)))
        {%GT;
<如果%(prop.HideSurroundingHtml)
            {%GT;
&所述;%= Html.Display(prop.PropertyName)%GT;
<%}
            其他
            {%GT;
<%,如果(!String.IsNullOrEmpty(prop.GetDisplayName()))
                {%GT;
                    <跨度类=显示标签>
&所述;%= prop.GetDisplayName()%>
                    < / SPAN>
<%}%GT;
                <跨度类=显示字段>
&所述;%= Html.Display(prop.PropertyName)%GT;
                < / SPAN>
<%}%GT;
<%}%GT;
<%}%GT;

最后啊可读性。唯一的问题是,它需要的办法的长期手工做到这一点。我需要一种方法来自动完成这个。某种code格式的解决方案。也许,宏或Visual Studio插件还是...?你有什么建议?

更新

我现在打算从我的标记重构了大部分的逻辑(见下文小李的答案),但在此期间,我想出了一个更易于管理的方式来格式化具有$ C的混合物的ascx文件$ c和HTML。在code更多的是小S $ P $垫了这种方式,但它更容易格式化code这样摆在首位,并将其与以及工作更容易。

 <%@控制语言=C#继承=System.Web.Mvc.ViewUserControl%GT;<%
    如果(型号== NULL)
    {
%GT;
        &所述;%= ViewData.ModelMetadata.NullDisplayText%GT;
<%
    }
    否则如果(ViewData.TemplateInfo.TemplateDepth→1)
    {
%GT;
        &所述;%= ViewData.ModelMetadata.SimpleDisplayText%GT;
<%
    }
    其他
    {
%GT;
<%
        在ViewData.ModelMetadata.Properties.Where的foreach(VAR道具(
            PM => pm.ShowForDisplay&功放;&安培; !ViewData.TemplateInfo.Visited(PM)))
        {
            如果(prop.HideSurroundingHtml)
            {
%GT;
                &所述;%= Html.Display(prop.PropertyName)%GT;
<%
            }
            其他
            {
%GT;
                < D​​IV CLASS =显示行>
<%
                如果(!String.IsNullOrEmpty(prop.GetDisplayName()))
                    {
%GT;
                        < D​​IV CLASS =显示标签>
                            &所述;%= prop.GetDisplayName()%>
                        < / DIV>
<%
                }
%GT;
                    < D​​IV CLASS =显示字段>
                        &所述;%= Html.Display(prop.PropertyName)%GT;
                    < / DIV>
            < / DIV>
<%
            }
        }
    }
%GT;


解决方案

我相信你的标签汤的痛苦其实是一个不同的问题的症状:你感觉它,因为你已经在你的观点得到了逻辑。意见应该非常轻便,很少或根本没有逻辑。你的逻辑应该在你的控制器,可以决定渲染取决于逻辑不同的看法。或者你可以把逻辑放到助手。

请参阅这篇文章罗布科纳

I was working on an ASP.NET MVC template today, and after staring at all those fluorescent yellow % tags for long enough, I basically decided I had had enough, so I painstakingly modified my ascx file to look like this:

<%@ 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>
<%          }                                                               %>
<%      }                                                                   %>
<%  }                                                                       %>

Ahh readability at last. The only problem is, it takes way to long to do this by hand. I need a way to automate this. Some kind of code formatting solution. Perhaps a macro or a Visual Studio add-in or ...? What do you advise?

Update

I'm now planning to refactor out most of the logic from my markup (see Mike's answer below), but in the mean time, I came up with a more manageable way to format ascx files that have a mixture of code and html. The code is a little more spread out this way, but it's much easier to format the code like this in the first place, and it's much easier to work with as well.

<%@ 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>
<%
            }
        }
    }
%>

解决方案

I believe your "tag soup" pain is actually a symptom of a different problem: you're feeling it because you've got logic in your view. Views should be very lightweight, with little or no logic. Your logic should be in your controllers, which can decide to render different views depending on the logic. Or you can put the logic into helpers.

See this article by Rob Conery

这篇关于在ASP.NET MVC&QUOT处理;标签汤&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆