7

I am trying to use the ternary operator in this piece of code, where Model.FirstTechSupportAssigneeElapseTime is of type TimeSpan?:

<dt>Assigned In</dt>
<dd> 
@if (@Model.FirstTechSupportAssigneeElapseTime == null)
     { @:N/A } 
else 
     { @Model.FirstTechSupportAssigneeElapseTime } 
</dd>

I have tried to implement the the ternary operator but I am failing miserably, the @'s everywhere are confusing me. Is it possible to have a the ternary operator in this scenario?

Thank you.

Simon MᶜKenzie
  • 8,344
  • 13
  • 50
  • 77
Guillermo Sánchez
  • 143
  • 1
  • 3
  • 15
  • Does this answer your question? [How to use ternary operator in razor (specifically on HTML attributes)?](https://stackoverflow.com/questions/4091831/how-to-use-ternary-operator-in-razor-specifically-on-html-attributes) – Michael Freidgeim Aug 17 '21 at 02:23

2 Answers2

14

Just keep in mind which scope you are in. Inside the if statement you do not need the @ because you are in c# scope. Inside of the conditional statement you are in razor scope, so you do need the @

<dt>Assigned In</dt>
<dd> 
@if (Model.FirstTechSupportAssigneeElapseTime == null)
{ 
    @:N/A 
} 
else 
{
    @Model.FirstTechSupportAssigneeElapseTime
} 
</dd>

This can also be done in using the ternary operator, assuming that elapsetime is a string (if it isn't there will be a conversion compilation error when the page loads)

<dt>Assigned In</dt>
<dd> 
@( Model.FirstTechSupportAssigneeElapseTime == null ? "N/A" : Model.FirstTechSupportAssigneeElapseTime.ToString() )
</dd>
Travis J
  • 81,153
  • 41
  • 202
  • 273
  • Actually ElapseTime is a TimeSpan and its complaining about this reason Operator ?? cannot be applied to TimeSpan and string. – Guillermo Sánchez Feb 06 '15 at 00:23
  • @GuillermoSánchez - The `??` will probably not work well then, you can still probably do the ternary operator if you use `.ToString()` on the timespan (see my edit) – Travis J Feb 06 '15 at 00:25
  • It tells me "left hand of the ?? operator shoild be of reference or nullable time" which is weird because the EllapseTime declaration is like this: public TimeSpan? FirstTechSupportAssigneeElapseTime { get; private set; } – Guillermo Sánchez Feb 06 '15 at 00:28
  • 1
    Great, the last suggetion worked @( Model.FirstTechSupportAssigneeElapseTime == null ? "N/A" : Model.FirstTechSupportAssigneeElapseTime.ToString() ), thank you very much – Guillermo Sánchez Feb 06 '15 at 00:33
4
<dt>Assigned In</dt>
<dd> 
    @(
        Model.FirstTechSupportAssigneeElapseTime == null 
        ? "N/A" 
        : Model.FirstTechSupportAssigneeElapseTime.ToString()  //per @Guillermo Sánchez's comment, it seems that FirstTechSupportAssigneeElapseTime is of type TimeSpan
                                                               //therefore the `.ToString()` was added to ensure that all parts of the if statement return data of the same type.
    )  
</dd>
JohnLBevan
  • 22,735
  • 13
  • 96
  • 178
  • 1
    I believe you have to use () instead of {} to get your example to work. – Nick Albrecht Feb 06 '15 at 00:14
  • For some reason this solution is not working, the compilation error returns: Type of conditional expression cannot be determined because there is no implicit conversion between 'string' and 'System.TimeSpan, I have tried with {} and () as well. – Guillermo Sánchez Feb 06 '15 at 00:18
  • Apologies; updated code with regular parentheses per @NickAlbrecht's comment. – JohnLBevan Jun 01 '17 at 14:35