@if (Model.Something != null)
{
<span id="SpanID">Model.SomethingElse</span>
}
else
{
<span id="SpanID">Model.SomethingElse2</span>
}
I am getting error. Another object on this page already uses ID 'SpanID'. I want to accomplish this by using ID only(and not using class) because lots of other things depend on that.
Declare a variable and use that within your span.
As Alex mentioned the best way is to move this logic to the controller and populate a
ViewModel
with only the data required for the view. This also makes more of your code testable.You could rewrite it:
Or use a ViewModel, moving the check from the View to the Controller (where it belongs, methinks).