alternating row color MVC

僤鯓⒐⒋嵵緔 提交于 2019-12-23 09:43:13

问题


I need to design a table with alternating row colors. Below is written code but its not working. May be some syntax issue for MVC. Please suggest.

@for (int i = 1; i <= 10; i++)

{

        var rowColor = "D9E6C4";
        <tr style="background-color:@rowColor;" >
            <td>apoorva</td>
        </tr>
        if (@rowColor.Equals("#ffffff"))
        {
            rowColor = "#D9E6C4";
        }
        else
        {
            rowColor = "#ffffff";
        }
}

回答1:


Take declaration of rowColor outside for statement.

@{ var rowColor = "D9E6C4"; }
@for (int i = 1; i <= 10; i++)
{
    <tr style="background-color:@rowColor;" >
        <td>
            apoorva
        </td>
    </tr>
    if (@rowColor.Equals("#ffffff"))
    {
        rowColor = "#D9E6C4";
    }
    else
    {
        rowColor = "#ffffff";
    }
}



回答2:


CSS3 example taken from http://davidwalsh.name/css-tables-css3-alternate-row-colors

tr:nth-child(odd)    { background-color:#ffffff; }
tr:nth-child(even)    { background-color:#D9E6C4; }



回答3:


Try...

@for (int i = 1; i <= 10; i++)
{
    string rowColor;
    if(i % 2 == 0)
    {
        rowColor = "D9E6C4";
    }
    else
    {
        rowColor = "ffffff";
    }
    <tr style="background-color:#@rowColor;" >
        <td>apoorva</td>
    </tr>
}



回答4:


You should use:

    if (rowColor.Equals("#ffffff"))
    {
        rowColor = "#D9E6C4";
    }
    else
    {
        rowColor = "#ffffff";
    }

An alternative is use a mod to choose the color:

    <tr style='background-color:@(i%2 == 0 ? "#D9E6C4":"#ffffff"  );'>
        <td>apoorva</td>
    </tr>



回答5:


This seems like a pretty basic error: each time through the loop, you're setting the value. Just move the initial set outside the loop:

var rowColor = "D9E6C4";
@for (int i = 1; i <= 10; i++)
{
    <tr style="background-color:@rowColor;" >
        <td>apoorva</td>
    </tr>
    if (@rowColor.Equals("#ffffff"))
    {
        rowColor = "#D9E6C4";
    }
    else
    {
        rowColor = "#ffffff";
    }
}

Edit: @jcreamer898 suggestion to use i % 2 is better than checking the color values.




回答6:


use css in your style

tr:nth-child(even) {background: #CCC}
tr:nth-child(odd) {background: #FFF}




回答7:


<html>
    <head>
        <title>Example Title</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $('tr:even').addClass('alt-row-class');
            });
        </script>
    </head>
    <body>...</body>
</html>

Then apply style to that class using standard css:

.alt-row-class { background-color: green; }

referenced form this previous post - https://stackoverflow.com/posts/663122/edit




回答8:


@{

 string rowColor = "#D9E6C4";   
 <table>
@for (int i = 1; i <= 10; i++)
{
        <tr style="background-color:@rowColor;" >
            <td>apoorva</td>
        </tr>
       rowColor = rowColor == "#D9E6C4" ? "#FFFFFF" : "#D9E6C4";
}
</table>
}


来源:https://stackoverflow.com/questions/8593477/alternating-row-color-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!