1

I have a model that I have little control over. It has a string property called Pass that contains a "1" to represent a pass, and a "0" to represent a fail.

I also have a display template that replaces boolean properties with a particular image, depending if the boolean value is true or false.

When I try to use @Html.DisplayFor(c => (c.Pass == "1")) in my Razor template, I get a

System.InvalidOperationException: Templates can be used only with field access, property access, single-dimension array index or single parameter custom indexer expressions.

How can I modify my view so that c.Pass == "1" is interpreted as a true value, and make use of the boolean display template that is available to me.

2 Answers 2

2

You can create a boolean variable in the view:

@{ var pass = Model.Pass == "1"; }
@Html.DisplayFor(c => pass)
Sign up to request clarification or add additional context in comments.

Comments

1

Use a custom display template.

1) In the model, add a attribute e.g.-

 [UIHint("PassFail")]
    public decimal Pass { get; set; }

2) Create a Display Template PassFail.cshtml @model string

@if (Model == "1")
{
    <text>Pass</text>
}
else
{
    <text>Fail</text>
}

3) Now you can directly use

@Html.DisplayFor(e => e.Pass)

Comments

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.