剃刀语法 - 如何有条件地包裹内的一些HTML [英] Razor syntax - how to conditionally wrap some inner HTML

查看:98
本文介绍了剃刀语法 - 如何有条件地包裹内的一些HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在剃刀,有大约只允许关闭一个奇怪的规则 HTML的如果块中。

In Razor, there's a curious rule about only allowing closed HTML within an if block.

请参阅:

剃刀不明白未闭合的HTML代码

不过,我有一个情况我要排除在某些情况下一些外,包裹元件。我不想重复所有的内部HTML,这是HTML和逻辑相当数量。

But I have a situation where I want to exclude some outer, wrapping elements under certain conditions. I don't want to repeat all the inner HTML, which is a fair amount of HTML and logic.

是解决问题的唯一方法,使另一个局部视图内的东西保持干燥?

没有任何其他的再利用这个新的部分,那感觉真是别扭,bloaty。我不知道规则是剃刀的限制或只是一个nannying(烦人)功能。

Without any other re-use for this new partial, it feels really awkward, bloaty. I wonder if the rule is a limitation of Razor or simply a nannying (annoying) feature.

推荐答案

您可以使用 Html.Raw(MyString的)。在的myString 你可以写任何你想要的,比​​如标签打开或关闭,而根本得到任何错误。即。

You can use Html.Raw(mystring). In myString you can write whatever you want, for example a tag opening or closing, without getting any errors at all. I.e.

if (condition) {
  @Html.Raw("<div>")
}

if (condition) {
  @Html.Raw("</div>")
}

注: @ Html.Raw(&LT; D​​IV&gt;中)可以写在一个较短的,另一种形式,像这样: @:其中; DIV&GT;

NOTE: the @Html.Raw("<div>") can be written in a shorter, alternative form, like this: @:<div>

您也可以创建自己的HTML辅助的简化剃刀语法。这些助手能接受的条件参数,这样就可以做这样的事情:

You can also create your own html helpers for simplifying the razor syntax. These helpers could receive the condition parameter, so that you can do something like this:

@Html.DivOpen(condition)

@Html.DivClose(condition)

或更复杂的助手让指定的属性,标记名称,等等。

or more complicated helpers that allow to specify attributes, tag name, and so on.

还是,无论是HTML佣工将被检测为标签,所以你可以自由地使用它们。

Nor the Raw, neither the html helpers will be detected as "tags" so you can use them freely.

做到这一点的最佳途径

这将是更安全的实现有点像 BeginForm HTML帮手。你可以看一下源$ C ​​$ C。这很容易实现:你只需要写在构造函数中开始标记,并在的Dispose 方法结束标记。这种技术的adavantage是,你不会忘记关闭有条件打开的标签。

It would be much safer to implement something like the BeginForm html helper. You can look at the source code. It's easy to implement: you simply have to write the opening tag in the constructor, and the closing tag in the Dispose method. The adavantage of this technique is that you will not forget to close a conditionally opened tag.

您需要实现这个HTML帮助(在静态类中声明的扩展方法):

You need to implement this html helper (an extension method declared in a static class):

  public static ConditionalDiv BeginConditionalDiv(this HtmlHelper html, 
    bool condition)
  {
      ConditionalDiv cd = new ConditionalDiv(html, condition);
      if (condition) { cd.WriteStart(); }
      return cd; // The disposing will conditionally call the WriteEnd()
  }

它采用了类像这样

Which uses a class like this:

  public class ConditionalDiv : IDisposable
  {
      private HtmlHelper Html;
      private bool _disposed;
      private TagBuilder Div;
      private bool Condition;

      public ConditionalDiv(HtmlHelper html, bool condition)
      {
          Html = html;
          Condition = condition;
          Div = new TagBuilder("div");
      }

      public void Dispose()
      {
          Dispose(true /* disposing */);
          GC.SuppressFinalize(this);
      }

      protected virtual void Dispose(bool disposing)
      {
        if (!_disposed)
        {
            _disposed = true;
            if (Condition) { WriteEnd(); }
        }
      }

       public void WriteStart()
       {
           Html.ViewContext.Writer.Write(Div.ToString(TagRenderMode.StartTag));
       }

       private void WriteEnd()
       {
           Html.ViewContext.Writer.Write(Div.ToString(TagRenderMode.EndTag));
       }
  }

您可以用相同的模式, BeginForm 使用。 (免责声明:本code未完全测试,但给它的工作原理可以接受属性额外的参数,标签名称等的想法)。

You can use this with the same pattern as BeginForm. (Disclaimer: this code is not fully tested, but gives an idea of how it works. You can accept extra parameters for attributes, tag names and so on).

这篇关于剃刀语法 - 如何有条件地包裹内的一些HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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