简体   繁体   English

html.raw没有呈现

[英]html.raw is not rendering

I'm trying to modify the background color of a table row using the code below. 我正在尝试使用以下代码修改表格行的背景色。 However, I'm getting a parser error because the closing tr tag doesn't have a matching opening tr. 但是,由于关闭tr标记没有匹配的开始tr,因此出现解析器错误。 I tried using @: instead of @Html.Raw(...), but I get the same error. 我尝试使用@:而不是@ Html.Raw(...),但遇到相同的错误。 If I type the tr tag outside of the conditional block, it renders a single tag and the table loads fine (just without alternating background colors). 如果我在条件块之外键入tr标签,它将呈现单个标签,并且表格加载良好(只是没有交替的背景色)。

<table>
    <tr>
        <th>Count</th>
        <th>Username</th>
    </tr>
 @foreach (var item in Model)
    {
        i++;
        if (i % 2 == 0){
            @Html.Raw("<tr>");
        }
        else{
            @Html.Raw("<tr style=\"background-color: #c9c9c9\">");
        }

        <td>@i.ToString()</td>
        <td>
            @Html.DisplayFor(modelItem => item.User)
        </td>
    </tr>
    }
</table>

You don't need @Html.Raw on those places. 在那些地方,您不需要@Html.Raw

Simply leave the tags as is <tr> 只需将标签保留为<tr>

Update... adding your code. 更新...添加您的代码。

<table>
    <tr>
        <th>Count</th>
        <th>Username</th>
    </tr>
 @foreach (var item in Model)
    {
        i++;
        if (i % 2 == 0){

            <tr>
        }
        else{
          <tr style='background-color: #c9c9c9'>
        }

        <td>@i.ToString()</td>
        <td>
            @Html.DisplayFor(modelItem => item.User)
        </td>
    </tr>
    }
</table>

Do this instead. 改为这样做。

<table>
    <tr>
        <th>Count</th>
        <th>Username</th>
    </tr>
    @foreach (var item in Model)
    {
        string backColor;
        i++;
        if (i % 2 == 0) {
            backColor = "whatever this color should be";
        }
        else {
            backColor = "#c9c9c9";
        }
    <tr style="background-color: @backColor">
        <td>@i.ToString()</td>
        <td>
            @Html.DisplayFor(modelItem => item.User)
        </td>
    </tr>
    }
</table>

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

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