Asp.net mvc ASP.NET MVC模板

Asp.net mvc ASP.NET MVC模板,asp.net-mvc,Asp.net Mvc,我对元素进行了重复标记,如: <div class="box"> <div class="top"></div> <div class="content"> content goes here </div> <div class="bottom"></div> </div> 内容在这里 顶部和底部使用css设置样式,以包含边框图像 我可以使用JQuer

我对元素进行了重复标记,如:

<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中没有内置的模板解决方案。但是,由于输出是纯html,因此只需使用css和一些JS就可以很容易地创建自己的模板


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

据我所知,asp.net mvc中没有内置的模板解决方案。但是,由于输出是纯html,因此只需使用css和一些JS就可以很容易地创建自己的模板


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

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

  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());
  }
publicstaticvoidbeginbox(此HtmlHelper HtmlHelper)
{
StringBuilder box=新的StringBuilder();
框。附录行(“”);
框。附录行(“”);
框。附录行(“”);
htmlHelper.ViewContext.HttpContext.Response.Write(box.ToString());
}
公共静态无效结束框(此HtmlHelper HtmlHelper)
{
StringBuilder box=新的StringBuilder();
框。附录行(“”);
框。附录行(“”);
框。附录行(“”);
htmlHelper.ViewContext.HttpContext.Response.Write(box.ToString());
}

我想现在就可以了。

我刚刚定义了两种扩展方法来编写标记

  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());
  }
publicstaticvoidbeginbox(此HtmlHelper HtmlHelper)
{
StringBuilder box=新的StringBuilder();
框。附录行(“”);
框。附录行(“”);
框。附录行(“”);
htmlHelper.ViewContext.HttpContext.Response.Write(box.ToString());
}
公共静态无效结束框(此HtmlHelper HtmlHelper)
{
StringBuilder box=新的StringBuilder();
框。附录行(“”);
框。附录行(“”);
框。附录行(“”);
htmlHelper.ViewContext.HttpContext.Response.Write(box.ToString());
}

我想现在就可以了。

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

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


如果您不使用此小助手,它将崩溃,因为它将尝试执行此操作。

解决此问题的方法可能有很多,这里有一种:

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


如果您不使用此小助手,它将崩溃,因为它将尝试强制执行操作。

您肯定希望对HtmlHelper使用扩展方法,以便使用表示法:

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


publicstaticstringbox(
这个HtmlHelper html,
字符串内容)
{
var sb=新的StringBuilder();
某人加上一行(“”);
某人加上一行(“”);
某人加上一行(“”);
(内容);
某人加上一行(“”);
某人加上一行(“”);
某人加上一行(“”);
使某人返回字符串();
}

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

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


publicstaticstringbox(
这个HtmlHelper html,
字符串内容)
{
var sb=新的StringBuilder();
某人加上一行(“”);
某人加上一行(“”);
某人加上一行(“”);
(内容);
某人加上一行(“”);
某人加上一行(“”);
某人加上一行(“”);
使某人返回字符串();
}

mvcView引擎的服务器控件实际上是模板引擎。看,我在想mvcView引擎的服务器控件实际上是模板引擎。看看为什么不让这些方法返回字符串中的html?为什么不让这些方法返回字符串中的html?如果“内容字符串”实际上是html而不仅仅是文本,或者如果我想包含对html.TextBox()等的调用,那么这会变得非常难看。True但是您可以使用其他扩展方法(beginBox和endBox)显示了blu在这种情况下的答案。如果您对两种解决方案都进行了编码,则会复制标记。。。如果“内容字符串”实际上是html而不仅仅是文本,或者如果我想包含对html.TextBox()等的调用,那么这将变得非常难看。如果是这样,那么你可以使用其他扩展方法(beginBox和endBox)显示了blu在这种情况下的答案。如果您对两种解决方案都进行了编码,则会复制标记。。。它将出现在blu的分机中,然后再次出现在您的分机中。
<%=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();
  }