我有一个名为InvoiceLine
的类,它具有以下属性。
public class InvoiceLine : IEntity
{
...
public virtual int? OfficeUserId { get; set; }
public virtual int? FieldUserId { get; set; }
public virtual bool? OfficeApproved { get; set; }
public virtual string OfficeRejectionReason { get; set; }
public virtual DateTime? OfficeApprovedDate { get; set; }
public virtual bool? FieldApproved { get; set; }
public virtual string FieldRejectionReason { get; set; }
public virtual DateTime? FieldApprovedDate { get; set; }
...
public virtual User OfficeUser { get; set; }
public virtual User FieldUser { get; set; }
...
}
我希望在表格中显示如下内容:
<td>Office sign off: Conan the Barbarian Approved</td>
<td>Field sign off: Steve the snake Not Approved</td>
所以我写道:
<td>Office sign off:
@Html.DisplayFor(modelItem => invoiceLine.OfficeUser.UserName)
@Html.DisplayFor(modelItem => invoiceLine.OfficeApproved)
</td>
<td>Field sign off:
@Html.DisplayFor(modelItem => invoiceLine.FieldUser.UserName)
@Html.DisplayFor(modelItem => invoiceLine.FieldApproved)
</td>
显然,这并没有提供所需的格式,并提供了类似的内容:
<td>
Office sign off: Conan the Barbarian
<select class="tri-state list-box" disabled="disabled">
<option value="">Not Set</option>
<option value="true" selected="selected">True</option>
<option value="false">False</option>
</select>
</td>
<td>
Office sign off: Steve the snake
<select class="tri-state list-box" disabled="disabled">
<option value="">Not Set</option>
<option value="true">True</option>
<option value="false" selected="selected">False</option>
</select>
</td>
所以我在下拉列表中获得批准的布尔值是有意义的。如我前面所述,如何让它显示?
答案 0 :(得分:2)
最简单的方法是忽略内置帮助程序并将其写出如下:
@( invoiceLine.FieldApproved.HasValue ? (invoiceLine.FieldApproved.Value ? "Approved" : "Not Approved"):"Not Specified" )
但是,你最好的办法就是编写自己的HTML处理程序(特别是如果这是一项常见任务,将在详细信息页面上使用等)。
public static MvcHtmlString ApprovedBoolFor<TModel, bool?>(this HtmlHelper<TModel> html, Expression<Func<TModel, bool?>> selector)
{
var prop = selector.Compile().Invoke(html.ViewData.Model);
var appVal = prop.HasValue ?
(prop.Value ? "Approved" : "Not Approved")
: "Not specified";
return new MvcHtmlString(appVal);
}