0

I have a class named InvoiceLine which has the following properties.

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; }

        ...
}

I want to display in a table something like:

<td>Office sign off: Conan the Barbarian Approved</td>
<td>Field sign off: Steve the snake Not Approved</td>

So I wrote:

<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>

Obviously this doesn't give the required format and delivers something like:

<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>

So I'm getting the boolean values for the approvals in dropdownlists which makes sense. How to I get it to display instead as I described earlier?

1 Answer 1

2

The easiest way to do this is just ignore the built in helpers and write it out as follows:

@( invoiceLine.FieldApproved.HasValue ? (invoiceLine.FieldApproved.Value ? "Approved" : "Not Approved"):"Not Specified" )

However, your best bet is to write your own HTML handler I think (especially if this is a common task and will be used on a details page etc).

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

that looks promising but I'm unsure where the selector is coming from.
I'm still not sure that's quite right because I'm getting squigglies under HasValue and Value. As in the compiler has no idea what they are
sorry, need to specify the type or cast it. I'll have to test the latest edit, but gotta run. will pop back in a bit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.