3
<span class="text-danger">
@{
    if (Model.uploadErrorMessage != null)
    {
        if (!string.IsNullOrEmpty(Model.uploadErrorMessage))
        {
            Model.uploadErrorMessage;
        }
    }
}
</span>

Given code above, how do I display Model.uploadErrorMessage inside <span> tag?

Currently it give me this error on Model.uploadErrorMessage:

Only assignment, call, increment, decrement, and new object expressions can be used as a statement

Pop
  • 525
  • 1
  • 7
  • 22

4 Answers4

5

Put your span tag inside if statement

@{
    if (Model.uploadErrorMessage != null)
    {
        if (!string.IsNullOrEmpty(Model.uploadErrorMessage))
        {
            <span class="text-danger">@Model.uploadErrorMessage</span>
        }
    }
}
Van VO
  • 637
  • 3
  • 10
4

All of those if statements are completely unnecessary anyway, you can simplify the entire thing to this:

<span class="text-danger">@Model.uploadErrorMessage</span>
DavidG
  • 113,891
  • 12
  • 217
  • 223
  • I may put at least one `if` that embrace the span and not adding an empty DOM to the page when the message is null or empty:-) – CodeNotFound Apr 27 '18 at 10:04
  • @CodeNotFound Well yes, around the `span` would be different, but it's possible the empty span is required due to some Javascript thing perhaps. – DavidG Apr 27 '18 at 10:05
0

use the "@"-symbol to write your variable value to the view:

for example:

<span>This is the error: @(Model.uploadErrorMessage)</span>
G43beli
  • 3,835
  • 4
  • 21
  • 28
-1

@Html.Raw(Model.uploadErrorMessage)

you can simply put @Model.uploadErrorMessage

Edit: XSS vulnerability fix, based on @DavidG's comment.

manish
  • 1,450
  • 8
  • 13