Auc
Auc

Reputation: 131

If statement is not evaluated correctly

I am trying to evaluate a property of a model to see if it is empty before attempting to render a HiddenFor HTML element, but the code block of the if statement is entered, even if the statement is false.

@{
if(Model.BoatImage.UploadedImagePath != string.Empty)
{
   @Html.HiddenFor(model => model.BoatImage.UploadedImagePath)
}
}

In the code above, UploadedImagePath is initialized to string.Empty in BoatImage's constructor. If I break at the if statement, Intellisense shows that UploadedImagePath is empty, and the Immediate window evaluates the if statement to be false. However, the program still attempts to render the HiddenFor element.

EDIT: The debugger incorrectly noted that the code above was causing the exception, when actually is was a couple of lines below the code above. Once I corrected the line that was causing the issue, the code above works correctly.

Upvotes: 0

Views: 113

Answers (1)

Dmitry Efimenko
Dmitry Efimenko

Reputation: 11203

in razor you should write like this:

@if(!string.IsNullOrEmpty(Model.BoatImage.UploadedImagePath))
{
   @Html.HiddenFor(model => model.BoatImage.UploadedImagePath)
}

Please note that I also changed your string evaluation method

Upvotes: 1

Related Questions