---
title: "Ternary operator in razor using a @Model"  
description: "Ternary operator in razor using a @Model"  
author: "Anonymous User"  
published: 2015-02-05  
updated: 2015-02-05  
canonical: https://www.mindstick.com/forum/12944/ternary-operator-in-razor-using-a-model  
category: "asp.net"  
tags: ["c#", "razor"]  
reading_time: 1 minute  

---

# Ternary operator in razor using a @Model

I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to use the [ternary operator](https://www.mindstick.com/forum/34080/how-to-use-ternary-operator-in-razor) in this piece of [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii), where Model.FirstTechSupportAssigneeElapseTime is of type [TimeSpan](https://www.mindstick.com/forum/12692/timespan-not-working)?:

```
<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](https://yourviews.mindstick.com/view/80697/bjp-losing-ally-everywhere) are confusing me. Is it possible to have a the ternary operator in this [scenario](https://yourviews.mindstick.com/view/81221/sports-will-change-the-education-scenario)?

Thank you.

## Replies

### Reply by Anonymous User

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](https://www.mindstick.com/blog/144/union-intersection-and-except-operator-in-sql-server), 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>
```


---

Original Source: https://www.mindstick.com/forum/12944/ternary-operator-in-razor-using-a-model

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
