Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If condition not working in HTML page with C# code

Tags:

html

c#

.net

razor

I'm using the following code to populate the table with data. The if statement is not working as i expected. Both the statements in the conditional blocks are executed.

  @if (--somecondition--)
  {
         <table>  
         foreach (Message userMessage in UserMessages)
         {                               
              <tr>
                  if(@userMessage.Message.MessageText.Length <= 10)
                  {
                      <td>
                          @userMessage.Message.MessageText
                      </td>
                  }
                  if(@userMessage.Message.MessageText.Length > 10)
                  {
                      <td>
                           @userMessage.Message.MessageText.Substring(0, 10)
                      </td>
                  }    
              </tr>        
         }
    </table>
}

What am i missing here? Isnt such use not possible?

EDIT (after seeing the answer):

I thought -

Once inside code, you do not need to prefix constructs like "if" with "@"

like image 788
Rohit Vipin Mathews Avatar asked Sep 05 '13 09:09

Rohit Vipin Mathews


People also ask

Why is my IF statement not working in C?

The short answer here is that you've written illegible code that you can no longer read. A few things to consider: (1) Use more, smaller, well-named functions (2) Use meaningful variable names (3) Make if statements that read like english.

How do you add an if else condition in HTML?

Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false. Use switch to specify many alternative blocks of code to be executed.

How does if work in C?

How if statement works? The if statement evaluates the test expression inside the parenthesis () . If the test expression is evaluated to true, statements inside the body of if are executed. If the test expression is evaluated to false, statements inside the body of if are not executed.


1 Answers

You need to start with @...

@foreach (Message userMessage in UserMessages)
{

and...

@if(userMessage.Message.MessageText.Length <= 10)
{

Without it at the start, the if( is still treated as HTML.


The @ symbol identifies the start of your Razor syntax (i.e. C# code) and will continue to be a razor code block until an appropriate terminator has been reached. There are a number of ways to move it back to HTML, the one most commonly seen in your example is to include a html tag, such as <td>.

Here is the complete version of your code, hopefully it will help you understand how it should work:

<table>
//due to the table tag, we are current inside HTML 
//so we need to use the @ symbol to move back to razor syntax
@foreach (Message userMessage in UserMessages)
{                               
    <tr>
    //using this tag again changes us back to HTML mode
    //so again we must use the at symbol
    @if(userMessage.Message.MessageText.Length <= 10)
    {
        //still Razor
        <td>
        //back in HTML mode
             @userMessage.Message.MessageText
        </td>
    }
    @if(userMessage.Message.MessageText.Length > 10)
    {
        <td>
             @userMessage.Message.MessageText.Substring(0, 10)
        </td>
    }    
    </tr>        
}
</table>

(I know these comments wont work in Razor so don't add them)


And to clear up what you initially thought. If you didn't have the first <tr> tag, then the following would work...

@foreach (Message userMessage in UserMessages)
{
   if(userMessage.Message.MessageText.Length <= 10)
   {

Notice how the whole if statement line doesn't required an @ symbol, because we never moved back to HTML mode.

like image 121
musefan Avatar answered Sep 22 '22 16:09

musefan