Abbreviation If not already in the Razor view (ASP.NET MVC4)

I try to check boolean and then show an integer:

@( ViewBag.HaveBeenHere ? submission.DurationInMonths )

I get an error message:

CS1003: Syntax error, ':' expected

I know that: for another, but in this case I have no other.

When I add it like this:

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

I get this error:

CS0173: The conditional expression type cannot be determined because there is no implicit conversion between 'int' and 'string'

How to make a reduction operator to check a boolean and display an integer in a view?

+4
source share
3 answers

You can do this if you want to keep the syntax:

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

ToString(), , .

+10

if-else, . :

@( if(ViewBag.HaveBeenHere) { submission.DurationInMonths; } )

. , , if.

+5

You do not have. Just use the operator if.

@( if(ViewBag.HaveBeenHere) submission.DurationInMonths; )

Conditional operator

The conditional operator (? :) returns one of two values depending on the value of the boolean expression.

+2
source

Source: https://habr.com/ru/post/1537937/


All Articles