Shorthand If without else in Razor view (ASP.NET MVC4)

asp.net, asp.net-mvc, asp.net-mvc-4, c#, razor

Solution

You could do this if you wanted to keep the syntax:

@( ViewBag.HaveBeenHere ? submission.DurationInMonths.ToString() : "" )

Adding `ToString()` will, of course, make the return types the same.

Problem

I am trying to check a boolean and then show an integer: ``` @( ViewBag.HaveBeenHere ? submission.DurationInMonths ) ``` I am getting an error: CS1003: Syntax error, ':' expected I know the : is for the else, but in this case I don't have an else. When I add it like this: ``` @( ViewBag.HaveBeenHere ? submission.DurationInMonths : "" ) ``` I get this error: CS0173: Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and 'string' How do I do a shorthand if statement to check a boolean and display an integer in the view?

Original source