asp.net-mvc - 在 System.Web.Helpers.WebGrid 中定义内联行样式

标签 asp.net-mvc asp.net-mvc-3 webgrid

我正在将我的应用程序迁移到 MVC 3 并尝试使用 System.Web.Helpers.WebGrid。我想得到像这样的html代码:

<table>
    <tr style="background-color: <%= item.Color %>">
    </tr>
    <tr style="background-color: <%= item.Color %>">
    </tr>
    <tr style="background-color: <%= item.Color %>">
    </tr>
</table>

rowStyle 属性,允许为每一行定义 css 类,但每一行会有不同的样式。是否容易实现?

最佳答案

所以我不得不以 hack 结束。首先,将颜色作为另一列的一部分。它必须作为 MvcHtmlString 返回以避免额外的编码:

<%  
    var grid = new WebGrid(Model);
%>
<%= 
    grid.GetHtml(
        tableStyle: "table_class",
        columns:  grid.Columns(
            grid.Column("Importance", CTRes.Importance, (item) => 
                new MvcHtmlString(Html.Encode(item.Importance) + 
                "<div class='color' style='display: none;'>#" + item.Color + "</div>"))
        )
    ) 
%>

然后我们在 $(document).ready() 中设置背景颜色:

<script type="text/javascript">
    $(document).ready(
        function () {
            $('.table_class .color').each(function (index, element) { $(element).parent().parent().css('background-color', $(element).html()); });
        }
    );
</script>

关于asp.net-mvc - 在 System.Web.Helpers.WebGrid 中定义内联行样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4946432/

相关文章:

sorting - 如何将默认排序顺序更改为降序?

ajax - 从 Ajax 表单加载的 MVC4 WebGrid - 排序和分页时多次调用 Controller

asp.net-mvc - 如何在 ASP.NET MVC 3 WebGrid 列名(标题)中添加 Html

c# - Ninject.ActivationException : Error activating IMainLicense 错误

jquery - 使用部分 View 和 Ajax 进行 ASP.NET MVC 客户端验证

asp.net-mvc - 将基于声明的授权添加到 MVC 3

asp.net - MVC 中最后修改的 header

c# - 格式化逻辑属于 MVC 的什么地方?

asp.net-mvc - ActionLink 扩展方法 MVC5 中的 htmlAttributes

javascript - 当 dataType 为 "json"时,成功回调不会运行