0

I have an ASP.NET MVC RAZOR View. This view had the following:

@Html.LabelFor(m => m.Name)

That approach was working fine. But, I needed to add a check on the name field.So I started using:

@if (String.IsNullOrWhiteSpace(Model.Name)
{
  <b>The name wasn't provided</b>
}
else
{
  Html.LabelFor(m => m.Name);
  <div>the name should appear</div>
}

Now, my label isn't appearing at all. In some cases, I'll see the "The name wasn't provided" like I expected. But, when a name is present, I see "the name should appear", but I do not see the actual name like I'd expect.

What am I doing wrong? It seems odd that just @Html.LabelFor(m => m.Name) worked as expected. But, once I added the check, it didn't.

1
  • I hope it solved your problem? Commented Apr 25, 2016 at 14:29

3 Answers 3

1
else
{
  @Html.LabelFor(m => m.Name);
  <div>the name should appear</div>
}
Sign up to request clarification or add additional context in comments.

2 Comments

Interesting. I didn't know you needed the @ when it was in a block like that.
@user687554 you do apparently
0

You have missed @ before Html.LabelFor

Comments

0

You need to add the @ symbol to Html part in the else section of your if statement:

@if (string.IsNullOrWhiteSpace(Model.Name)
{
     <b>The name wasn't provided</b>
}
else
{
     @Html.LabelFor(m => m.Name);
     <div>the name should appear</div>
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.