【问题标题】:ASP.NET MVC TemplatingASP.NET MVC 模板
【发布时间】:2009-03-07 21:14:18
【问题描述】:

我对元素有重复的标记,例如:

<div class="box">
    <div class="top"></div>
    <div class="content">
      content goes here
    </div>
    <div class="bottom"></div>
</div>

顶部和底部使用 css 设置样式以包含边框图像。

我可以使用 JQuery 来注入标签,但是在 asp.net mvc 中模板控件是否有更好的方法?

谢谢

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    可能有很多方法可以解决这个问题,这里有一个:

    创建一个 ViewUserControl(我们称之为“box.ascx”):

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Action>" %>
    <div class="box">
        <div class="top"></div>
        <div class="content">
            <% Model(); %>
        </div>
        <div class="bottom"></div>
    </div>
    

    在你的 aspx 中,只要你需要这个块,就这样调用它:

    <% Html.RenderPartial("box", Lambda.Action(() => { %>
    here comes my content! <a href="http://www.google.com">Google!</a>
    <% })); %>
    

    这是 Lambda 助手类:

    public static class Lambda {
        public static Action Action(Action a) {
            return a;
        }
    }
    

    如果你不使用这个小助手,它会崩溃,因为它会尝试施放动作。

    【讨论】:

      【解决方案2】:

      我刚刚定义了两个扩展方法来写标签

        public static void BeginBox(this HtmlHelper htmlHelper)
        {
            StringBuilder box = new StringBuilder();
      
            box.AppendLine("<div class=\"box\">");
            box.AppendLine("    <div class=\"top\"></div>");
            box.AppendLine("        <div class=\"content\">");
      
            htmlHelper.ViewContext.HttpContext.Response.Write(box.ToString());
        }
      
        public static void EndBox(this HtmlHelper htmlHelper)
        {
            StringBuilder box = new StringBuilder();
      
            box.AppendLine("        </div>");
            box.AppendLine("    <div class=\"bottom\"></div>");
            box.AppendLine("</div>");
      
            htmlHelper.ViewContext.HttpContext.Response.Write(box.ToString());
        }
      

      我想现在就可以了。

      【讨论】:

      • 为什么不让方法以字符串形式返回 html?
      【解决方案3】:

      您肯定希望使用 HtmlHelper 的扩展方法,以便您可以使用该符号:

      <%=Html.Box("content string") %>
      

      public static stringBox(
             this HtmlHelper html, 
             string Content)
        {
            var sb = new StringBuilder();
      
            sb.AppendLine("<div class=\"box\">");
            sb.AppendLine("    <div class=\"top\"></div>");
            sb.AppendLine("        <div class=\"content\">");
            sb.AppendLine(Content);
            sb.AppendLine("        </div>");
            sb.AppendLine("    <div class=\"bottom\"></div>");
            sb.AppendLine("</div>");
      
            return sb.ToString();
        }
      

      【讨论】:

      • 如果“内容字符串”实际上是 html 而不仅仅是文本,或者我想包含对 Html.TextBox() 等的调用,这将变得非常难看。
      • 是的,但您可以使用其他扩展方法(beginBox 和 endBox)显示蓝光针对这种情况的答案。
      • 如果你对这两种解决方案都进行了编码,那么你的标记就会被复制……它会出现在 blu 的扩展中,然后又出现在你的扩展中。
      【解决方案4】:

      据我所知,asp.net mvc 中没有内置模板解决方案。但是因为输出是纯 html,所以很容易用 css 和一些 JS 来做你自己的模板。

      当然,因为 asp.net mvc 中的所有内容都是可交换的,您可以将默认视图引擎替换为支持模板的视图引擎。

      【讨论】:

      【解决方案5】:

      为什么不使用母版页?您可以像在 ASP.Net 中一样在 MVC 中使用母版页

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-11-14
        • 2010-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多