0

Why there is a Visual Studio message about redundant conditional ternary expression usage, when there is a mouse over expression (also expression "true : false" has lower opacity). Does it tell me about i can write somehow less code to get what i want (if ViewBag.CMSClientStatus is equal to "Действующий", then check the radio)?

@Html.RadioButton("ClientStatus", "Real",  ViewBag.CMSClientStatus == "Действующий" ? true : false, new { @readonly = true })

Screenshot

olly
  • 63
  • 9
  • Why you even want to use ternary? do you miss parenthesis or something? – M.kazem Akhgary Jun 30 '15 at 18:18
  • What is it that you are trying do do? Best guess is that you want to make the control readonly if `ViewBag.CMSClientStatus == "Действующий"` (if that's the case then it would need to be `@Html.RadioButton("ClientStatus", "Real", ViewBag.CMSClientStatus == "Действующий" ? new { @readonly = true }: null)` –  Jul 01 '15 at 05:27

3 Answers3

2

Expression ViewBag.CMSClientStatus == "Действующий" already returns true or false. Ternary operator adds nothing to your logic, it's redundant as Visual Studio suggest.

Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
1

Instead of:

ViewBag.CMSClientStatus == "Действующий" ? true : false

Just use:

ViewBag.CMSClientStatus == "Действующий"

The equality operator (==) already returns true or false, so no need for a conditional operator.

Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
  • wait. this is the most simple thing in c#. how could he write code when he dont know `==` returns true and false (and he knows ternary ?!). i think he miss parenthesis or something – M.kazem Akhgary Jun 30 '15 at 18:20
0

I've write as it was suggested in previous two answers. But then an error came out: "Extension methods cannot be dynamically dispatched".

Screenshot

Answer was found here. I need cast the dynamic type to Boolean type. My final entry is:

@Html.RadioButton("ClientStatus", "Потенциальный",  (bool) (ViewBag.CMSClientStatus == "Потенциальный"), new { @readonly = true })   
Community
  • 1
  • 1
olly
  • 63
  • 9