用HTML.Raw和li解析错误?

时间:2013-04-15 18:04:56

标签: asp.net html asp.net-mvc razor

我在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>

1 个答案:

答案 0 :(得分: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;\"" : "")>