在ASP.MVC WebGrid中更改标签文本和标签背景颜色

时间:2015-08-04 15:03:52

标签: asp.net

我正在将ASP.NET webform应用程序重写为ASP.NET MVC。在webform中,有一个gridview,其中包含以下gridview列代码:

<asp:TemplateField HeaderText="PREV STAT MISSION"  ItemStyle-HorizontalAlign="Center" >
    <ItemTemplate>
        <asp:Label ID="lblDateCompleted" runat="server" ForeColor='<%# Item.PREV_STAT_MISSION == 4 ? System.Drawing.Color.White : System.Drawing.Color.Black   %>'   BackColor= '<%# Item.PREV_STAT_MISSION == 1 ? System.Drawing.Color.Green : Item.PREV_STAT_MISSION == 2 ? System.Drawing.Color.Yellow : Item.PREV_STAT_MISSION == 3 ? System.Drawing.Color.Red : Item.PREV_STAT_MISSION == 4 ? System.Drawing.Color.Black : Item.PREV_STAT_MISSION == 5 ? System.Drawing.Color.Blue : System.Drawing.Color.Transparent  %>'      Font-Bold="true" Text='<%# Item.PREV_STAT_MISSION == 1 ? " F1 " : Item.PREV_STAT_MISSION == 2 ? "  F2 " : Item.PREV_STAT_MISSION == 3 ? " F3 " : Item.PREV_STAT_MISSION == 4 ? " F4 " : Item.PREV_STAT_MISSION == 5 ?  " F5 " : "" %>' />
    </ItemTemplate>
</asp:TemplateField>

我正在使用ASP.NET MVC WebGrid。到目前为止,我重写了像这样的代码

grid.Column("PREV_STAT_MISSION", format: @<text> <span class="display-mode"></span><label id="lblPrevStatMission" class="edit-mode">
    @item.PREV_STAT_MISSION == 1 ? " F1 " : @item.PREV_STAT_MISSION == 2 ? "  F2 " : @item.PREV_STAT_MISSION == 3 ? " F3 " : @item.PREV_STAT_MISSION == 4 ? " F4 " : @item.PREV_STAT_MISSION == 5 ? " F5 " : "" </label></text>),

但它不起作用。我不知道如何使用Grid.Column更改标签文本和颜色。任何帮助是极大的赞赏。感谢。

1 个答案:

答案 0 :(得分:0)

@ @item.PREV_STAT_MISSION == 1 ? " F1 " : @item.PREV_STAT_MISSION == 2 ? " F2 " : @item.PREV_STAT_MISSION == 3 ? " F3 " : @item.PREV_STAT_MISSION == 4 ? " F4 " : @item.PREV_STAT_MISSION == 5 ? " F5 " : "" 

改为 o => o.PREV_STAT_MISSION == 1?" F1 " ...

就像这里@grid.GetHtml(columns: grid.Columns(grid.Column("name",format:o => o.Name=="F1"?"F!!":"F22")))

一样