【问题标题】:VisualStudio auto-format doesn't format my razor code correctlyVisualStudio 自动格式化无法正确格式化我的剃须刀代码
【发布时间】:2015-08-20 03:58:40
【问题描述】:

我正在使用@:</div> 正确显示一些引导列。这是我正在使用它的代码:

var i = 0;

<div class="container-fluid">
    <div class="row show-grid">
        @foreach (var one in Model)
        {

            if (i % 3 == 0)
            {
                @:<div class="row show-grid">
            }

            <div class="one-element col-md-4">
                @one.Title
            </div>

            if ((i + 1) % 3 == 0)
            {
                @:</div>
            }

            i++;
        }
    </div>
</div>

它对此进行格式化(只要我不使用VisualStudio 自动格式化功能,它就可以正常工作):

@:</div>

到这里:

@:
</div>

然后应用程序就不再工作了。

如何解决这个问题?

【问题讨论】:

  • 旁注:我认为按 3 分组并使用普通 Razor 构造进行渲染而不是使用不匹配的标签进行黑客攻击会更好......但确实是一种选择。
  • @AlexeiLevenkov 听起来不错。能否提供示例或示例链接?
  • items.Select((value, index)=&gt;new { value, index}).GroupBy(x=&gt;x.index / 3) 或以stackoverflow.com/questions/29606945/…开头的许多其他变体...搜索 - bing.com/search?q=c%23+enumerable+buckets
  • 我用过Codemaidcodemaid.net

标签: c# asp.net-mvc razor


【解决方案1】:

我使用@Html.Raw() 修复了它,如下所示:

var i = 0;

<div class="container-fluid">
    <div class="row show-grid">
        @foreach (var one in Model)
        {

            if (i % 3 == 0)
            {
                @Html.Raw("<div class=\"row show-grid\">")
            }

            <div class="one-element col-md-4">
                @one.Title
            </div>

            if ((i + 1) % 3 == 0)
            {
                @Html.Raw("</div>")
            }

            i++;
        }
    </div>
</div>

我想这已经是最好的了。

但如果有人知道更优雅的方法,请告诉我。

【讨论】:

    【解决方案2】:

    好的,我明白了,我的 &lt;text&gt; 标签和 here is why 错了:

    在 Razor 中,标签必须正确嵌套。 &lt;text&gt;&lt;/div&gt;&lt;/text&gt; 不是 正确的嵌套。

    @Alexei Levenkov 提到的解决问题的最佳方法:

    <div class="container-fluid">
        <div class="row show-grid">
            @foreach (var one in Model.Select((value, index) => new { value, index }).GroupBy(x => x.index / 3))
            {
                <div class="row show-grid">
                    @foreach (var el in one.Select(x => x.value))
                    {
                        <div class="one-element col-md-4">
                            @el.Title
                        </div>
                    }
                </div>
            }
        </div>
    </div>
    

    但是,根据this answer,您的Html.Raw() 方法已经足够了。

    【讨论】:

    • 您刚刚复制了Alexei Levenkov 的回答/评论。我认为他应该得到荣誉。也许他甚至会把它写成答案。
    【解决方案3】:

    这里的问题似乎是您在if (i % 3 == 0) 条件下打开&lt;div&gt;,但您没有在相同条件下关闭&lt;/div&gt; if ((i + 1) % 3 == 0)
    这意味着您可以有一个永远不会关闭的打开 &lt;div&gt; 或一个永远不会打开的关闭 &lt;/div&gt;
    也许你可以试试这个:

    var i = 0;
    
    <div class="container-fluid">
      <div class="row show-grid">
        @foreach (var one in Model)
        {
            <div class="one-element col-md-4">
                @one.Title
            </div>
    
            if (i % 3 == 0)
            {
               <div class="clear"></div>
            }
    
            i++;
        }
      </div>
    </div>
    

    可能这就是你遇到麻烦的地方! 我更新了你的评论。 更新@Alexei Levenkov 评论。我也在寻找一种更好的方法来做到这一点。当我需要每行 3 列时,这个解决方案就是我在项目中的做法。

    【讨论】:

    • 我知道...但是如果我在 if 语句中打开和关闭标签,我应该如何让每 1 行的 3 列工作?
    • 我更新了代码以反映每行 3 列。这段代码对我有用。您可以在 3 个元素后清除行,这会将您带回到新行。
    • 考虑删除关于 OP 代码正确性的完全不公平的声明...也不是说您的更改会产生与 OP 完全不同的标记 - 所以我认为反对票仍然是合理的。
    • 谢谢。我想这也可以,但这不是引导标准:getbootstrap.com/css/#grid-options 我希望引导代码是干净的(没有黑客)。
    猜你喜欢
    • 2011-02-07
    • 2022-11-30
    • 2012-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-23
    • 1970-01-01
    • 2017-04-10
    相关资源
    最近更新 更多