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

时间:2016-05-10 19:14:03

标签: c# asp.net asp.net-mvc razor

我有用于在Razor中遍历的特殊模板。模板从PHP迁移,100%有效。 Bud Razor显示错误:

  

遇到结束标记“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>

5 个答案:

答案 0 :(得分:0)

是的,你可以,转到工具 - &gt;选项 - &gt;文字编辑器 - &gt; Html - &gt;验证并关闭它。

答案 1 :(得分:0)

使用@Html.Raw方法插入字符串,不进行任何验证。显然你不能在这里使用特定于Razor的技巧。

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

答案 2 :(得分:0)

您可以在代码前使用@:

答案 3 :(得分:0)

一个选项是输出字符串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语法,并重新编写页面以使其正常工作&#34;正确&#34;办法。您似乎正在尝试基于递归结构创建嵌套列表。如果是这种情况,您可以创建一个递归调用自身的if/else函数,而不是拥有一堆令人困惑的for块和@helper循环。毫无疑问,这种方法可以产生更清晰,更不易出错的代码。

答案 4 :(得分:0)

决赛:

<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++;
    }

Thanx for all