How do you change a value on the fly in an ASP.Net MVC WebGrid column?
I have a ASP.Net MVC Webgrid and when the 'SuccessRate' column is less than zero, I need to display 'N/A'. Do you know how I would do this? Following is part of my cshtml:
@grid.GetHtml(
tableStyle: "grid",
headerStyle: "head",
alternatingRowStyle: "alt",
columns: grid.Columns(
grid.Column("ABC"),
grid.Column("Units",开发者_Go百科 "units"),
grid.Column("Min_Req_Res_Points", "mrrp"),
grid.Column("Min_Req_NRes_Points", "mrnrp"),
grid.Column("Total_Applications_Available", "avail"),
grid.Column("Total_Applications_Submitted", "total"),
grid.Column("Season_Number", "Season"),
grid.Column("Year"),
grid.Column(columnName: "SuccessRate",
format: @<text>@item.Success_Rate</text>)
I don't have a testbed I can use to test this example, but is it possible to use the ?: operator?
format: @<text>@item.Success_Rate >= 0 item.Success_Rate : "N/A"</text>
That, or you can use a view model that supplies the correct string representation of the value.
public string Success_Rate_Text
{
get
{
return this.Success_Rate >= 0 ? SuccessRate.ToString : "N/A";
}
}
I am not at a computer with a C# compiler at the moment, but you should be able to do an conditional check right within the column definition. Something like this:
grid.Column(columnName: "SuccessRate",
format: @if(item.Success_Rate < 0) { <text>N/A</text> }
else { item.Success_Rate })
精彩评论