C# MVC3 Razor 在@foreach 列表中交替项目? [英] C# MVC3 Razor alternating items in a @foreach list?

查看:21
本文介绍了C# MVC3 Razor 在@foreach 列表中交替项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 MVC3 中,如何在使用 Razor 视图引擎时在 @foreach 列表上创建交替的行颜色?

In MVC3, how do you create alternating row colors on a @foreach list when using the Razor view engine?

@foreach (var item in Model) {    
    <tr>
        <td>@item.DisplayName</td>
        <td>@item.Currency</td>
        <td>@String.Format("{0:dd/MM/yyyy}", item.CreatedOn)</td>
        <td>@String.Format("{0:g}", item.CreatedBy)</td>
        <td>@Html.ActionLink("Edit", "Edit", new { id = item.Id })</td>
    </tr>
}

推荐答案

这就是 CSS 的用途(改变样式而不是内容).省去服务器的工作量:在客户端上做.

This is what CSS is for (changing style rather than content). Save the server the effort: Do it on the client.

由于您使用的是 Razor,因此您可以使用 JQuery.以下是我在项目中的做法:

Since you're using Razor, you can use JQuery. Here's how I do it in my projects:

$(document).ready(function () {
    $("table > tbody tr:odd").css("background-color", "#F7F7F7");
}

这篇关于C# MVC3 Razor 在@foreach 列表中交替项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆