繁体   English   中英

我需要忽略Razor中的所有错误

[英]I need to ignore all errors in Razor

我有用于在Razor中遍历的特殊模板。 模板是从PHP迁移而来的,可以100%工作。 芽剃刀显示错误:

遇到结束标签“ li”,没有匹配的开始标签。 您的开始/结束标签是否正确平衡?

我可以强制忽略开始/结束标签吗?

我的代码:

<ul id="filter">

    @{ 
        List<Nipo.Models.Entity.category_path> CategoryPaths = ViewData["Categories"] as List<Nipo.Models.Entity.category_path>;
        Nipo.Models.Entity.category CurrentCategory = ViewData["Category"] as Nipo.Models.Entity.category;
        Dictionary<int, List<int>> Parents = ViewData["Parents"] as Dictionary<int, List<int>>;
        int Counter = 0;
    }

    @foreach (var CategoryPath in CategoryPaths)
    {
        Nipo.Models.Entity.category_path Next = CategoryPaths[Counter + 1];
        bool IsActive = (CurrentCategory != null && CurrentCategory.id_category == CategoryPath.category.id_category);

        <li class="if(IsActive) { active }">
            <a title="CategoryPath.category.name">
                CategoryPath.category.name
                <i class="fa fa-caret-right"></i>
                <i class="fa fa-caret-down"></i>
            </a>


            if (Next != null && Next.depth > CategoryPath.depth)
            {
                <ul class="collapse if (IsActive) { in } if (Parents[CategoryPath.category.id_category] != null && CurrentCategory != null && Parents[CategoryPath.category.id_category].Contains(CurrentCategory.id_category)) { style="display: block" })
            }else if (Next != null && Next.depth < CategoryPath.depth)
            {
                for (var i = 0; i < (CategoryPath.depth - Next.depth); i++)
                {
                    </ul></li>
                }
            }else if (Next != null && Next.depth == CategoryPath.depth)
            {
                </li>
            }else if(Next == null)
            {
                for(var i = 0; i < CategoryPath.depth; i++)
                {
                    </ul></li>
                }
            }
    }

</ul>

是的,可以,转到工具->选项->文本编辑器-> HTML->验证,然后将其关闭。

使用@Html.Raw方法插入没有任何验证的字符串。 显然,您不能在此处使用Razor特有的技巧。

或者,更简单地说,将结束标记移到末尾,因为在两种情况下都会重复结束标记。

您可以在标签前面使用@:

一种选择是输出字符串Raw,而不是作为Razor的一部分,如下所示:

@foreach (var CategoryPath in CategoryPaths)
{
    Nipo.Models.Entity.category_path Next = CategoryPaths[Counter + 1];
    bool IsActive = (CurrentCategory != null && CurrentCategory.id_category == CategoryPath.category.id_category);

    <li class="if(IsActive) { active }">
        <a title="CategoryPath.category.name">
            CategoryPath.category.name
            <i class="fa fa-caret-right"></i>
            <i class="fa fa-caret-down"></i>
        </a>


        if (Next != null && Next.depth > CategoryPath.depth)
        {
            <ul class="collapse if (IsActive) { in } if (Parents[CategoryPath.category.id_category] != null && CurrentCategory != null && Parents[CategoryPath.category.id_category].Contains(CurrentCategory.id_category)) { style="display: block" })
        }else if (Next != null && Next.depth < CategoryPath.depth)
        {
            for (var i = 0; i < (CategoryPath.depth - Next.depth); i++)
            {
                </ul>
                @Html.Raw("</li>")
            }
        }else if (Next != null && Next.depth == CategoryPath.depth)
        {
            @Html.Raw("</li>")
        }else if(Next == null)
        {
            for(var i = 0; i < CategoryPath.depth; i++)
            {
                </ul>
                @Html.Raw("</li>")
            }
        }
}

但是,我强烈建议您采用Razor语法,然后重新编写页面以使此工作“正确”地进行。 看起来您正在尝试基于递归结构创建嵌套列表。 如果是这样,您可以创建一个@helper函数来递归地调用它,而不是使用一堆混乱的if/else块和for循环。 无疑,这种方法将产生更干净,更不易出错的代码。

最后:

<ul id="filter">

    @{ 
        List<Nipo.Models.Entity.category_path> CategoryPaths = ViewData["Categories"] as List<Nipo.Models.Entity.category_path>;
        Nipo.Models.Entity.category CurrentCategory = ViewData["Category"] as Nipo.Models.Entity.category;
        Dictionary<int, List<int>> Parents = ViewData["Parents"] as Dictionary<int, List<int>>;
        int Counter = 0;
    }

    @foreach (var CategoryPath in CategoryPaths)
    {


        Nipo.Models.Entity.category_path Next = CategoryPaths.ElementAtOrDefault(Counter+1);
        bool IsActive = (CurrentCategory != null && CurrentCategory.id_category == CategoryPath.category.id_category);

        if (IsActive)
        {
            @Html.Raw("<li class=\"active\">")
        }
        else
        {
            @Html.Raw("<li>")
        }

        <a title="@CategoryPath.category.name">
            @CategoryPath.category.name
            <i class="fa fa-caret-right"></i>
            <i class="fa fa-caret-down"></i>
        </a>


        if (Next != null && Next.depth > CategoryPath.depth)
        {

            string Class = "";
            string Style = "";
            string Element = "<ul class=\"collapse ";

            if (IsActive)
            {
                Element += "in ";
            }

            Element += "\" ";

            if (Parents.ContainsKey(CategoryPath.category.id_category) && CurrentCategory != null && Parents[CategoryPath.category.id_category].Contains(CurrentCategory.id_category))
            {
                Element += "style=\"display: block\" ";
            }

            @Html.Raw(Element)

        } else if (Next != null && Next.depth < CategoryPath.depth)
        {
            for (var i = 0; i < (CategoryPath.depth - Next.depth); i++)
            {
                    @Html.Raw("</ul></li>");
                }
            }
            else if (Next != null && Next.depth == CategoryPath.depth)
            {
                @Html.Raw("</li>");
            }
            else if (Next == null)
            {
                for (var i = 0; i < CategoryPath.depth; i++)
                {
                    @Html.Raw("</ul></li>");
                }
        }
        Counter++;
    }

感谢所有人

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM