1

我的 ASP.NET MVC 网站中有以下代码:

@foreach (var item in Model.Comments.Where(c=>c.CommentId == Model.CurrentNode))
{
    if(Model.Level == 0){
        @Html.Raw("<li style=\"border:none;\">")
    }
    else
    {
        @Html.Raw("<li>")
    }

尝试访问此页面时,出现以下异常:

解析器错误消息:遇到没有匹配开始标记的结束标记“li”。您的开始/结束标签是否正确平衡?

我尝试了很多不同的选项,但无法让它发挥作用。

编辑1:

@if (!Model.CurrentNode.HasValue)
{
    @Html.Raw("<div class=\"comment-root\" id=\"comment-root\">")
    @*hid911 Tells what voteKey to use for this section*@
    <input pid="hidVoteKey" type="hidden" value="1" />
}
<ul class="commentList sub">
@foreach (var item in Model.Comments.Where(c=>c.CommentId == Model.CurrentNode))
{
    if(Model.Level == 0){
        <li style="border:none;">
    }
    else
    {
        <li>
    }


            @if(!item.Deleted)
            {
                <div class="commentBack">
                    @if (Request.IsAuthenticated && uFeed.Handlers.AccountHandler.UserContextInstance.Id != item.CreatedBy.Id)
                    {
                        @Html.Partial("VotePartial", item.VoteViewModel)    
                    }
                    else                    
                    {
                        <div style="float:left; width: 30px; ">&nbsp;</div>                                                
                    }
                    <div class ="floatLeft" style="width: 90%">
                        <div class="CommentHeader" id="commentHeader-@item.Id">
                            <p class="Timestamp dimText">
                                @Html.ActionLink(item.CreatedBy.Text, "User/" + item.CreatedBy.Text, "Post")
                                | @item.VoteBalance  poäng | 
                                @Html.GetDisplayDate(item.CreatedDate)
                            </p>

                        </div>
                        <div id="commenttext-@item.Id" class="commentText"> 
                            @item.Text
                        </div>
                        <div id="comment-@item.Id" class="CommentFooter">
                            @if (Request.IsAuthenticated)
                            {
                                <div  class="floatLeft">
                                    @Html.ActionLink("Svara", string.Empty, null, new { id= "respond-" + item.Id, href = "#comment", @class = "respond" })
                                </div>  
                            }                                 
                            @if (Request.IsAuthenticated && uFeed.Handlers.AccountHandler.UserContextInstance.Id ==item.CreatedBy.Id)
                            {
                                <div class="floatLeft" id="divremove-@item.Id">         
                                &nbsp; - &nbsp;@Html.ActionLink("Editera",  string.Empty, null, new { id= "edit-" + item.Id, href = "#comment", @class = "edit" })
                                &nbsp; - &nbsp;@Html.ActionLink("Ta bort", string.Empty, null, new { id="remove-"+item.Id, @class = "remove"})
                                </div>                                                     
                                <div  class="floatLeft hidden" id="divconfirmremove-@item.Id">&nbsp; - &nbsp;Ta bort?
                                @Html.ActionLink("Ja", string.Empty, null, new { id = "confirm-remove-yes-" + item.Id, @class = "confirm-remove-yes" })
                                    /                                                                           
                                @Html.ActionLink("Nej", string.Empty, null, new { id = "confirm-remove-no-" + item.Id, @class = "confirm-remove-no" })
                                </div>

                            }

                            @if(Model.Comments.Any(c=>c.CommentId == item.Id))
                            { 
                                @Html.Raw("&nbsp; - &nbsp;")
                                @Html.ActionLink("Dölj kommentarer", string.Empty, null, new { id = "toggle-" + item.Id, Href = "#togglecomments", @class = "toggle active" })
                            }
                            <div style="clear:both;"></div>     
                        </div>
                    </div>
                    <div style="clear:both;"></div>
                </div>
            }
            else
            {
                <div class="commentBack">
                    <div style="float:left; width: 30px; height: 30px">&nbsp;</div>
                        <div style="float:left">
                            <div class="commentText" ><i>[Borttagen]</i></div>
                            <div id="comment-@item.Id" class="CommentFooter">
                                @if(Request.IsAuthenticated)
                                {
                                    <div  class="floatLeft">
                                        @Html.ActionLink("Svara", string.Empty, null, new { id= "respond-" + item.Id, href = "#comment", @class = "respond" })
                                    </div> 
                                }
                                @if(Model.Comments.Any(c=>c.CommentId == item.Id))
                                { 
                                    @Html.Raw("&nbsp; - &nbsp;")
                                    @Html.ActionLink("Dölj kommentarer", string.Empty, null, new { id = "toggle-" + item.Id, Href = "#togglecomments", @class = "toggle active" })
                                }
                            </div>
                         </div>

                    <div style="clear:both;"></div>

                </div>
            }
            <div id="comments-for-@item.Id" class="CommentContainer">
                    @Html.Partial("CommentPartial", new uFeed.Views.ViewModels.CommentTreeItemViewModel(item.Id, Model.Comments, Model.Level + 1))
            </div>

            <div style="clear:both;"></div>
    </li>
}
</ul>
4

1 回答 1

2

您不需要 Html.Raw,因为您实际上并未输出包含 HTML 的代码变量。这应该可以正常工作:

@foreach (var item in Model.Comments.Where(c=>c.CommentId == Model.CurrentNode))
{
    if(Model.Level == 0){
       <li style="border:none;">
    }
    else
    {
       <li>
    }

你可能可以进一步简化这个......

@foreach (var item in Model.Comments.Where(c=>c.CommentId == Model.CurrentNode))
{
    <li @(Model.Level == 0 ? "style=\"border:none;\"" : "")>
于 2013-04-15T18:56:41.940 回答