简体   繁体   English

如果条件不适用于带有C#代码的HTML页面

[英]If condition not working in HTML page with C# code

I'm using the following code to populate the table with data. 我正在使用以下代码用数据填充表。 The if statement is not working as i expected. if语句没有像我预期的那样工作。 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 "@" 进入代码后,您不需要使用“@”作为“if”的结构前缀

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. 如果没有它,那么if(仍被视为HTML。


The @ symbol identifies the start of your Razor syntax (ie C# code) and will continue to be a razor code block until an appropriate terminator has been reached. @符号标识Razor语法的开头(即C#代码),并将继续成为剃刀代码块,直到达到适当的终结符。 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> . 有许多方法可以将其移回HTML,在您的示例中最常见的方法是包含一个html标记,例如<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) (我知道这些评论在Razor中不起作用所以不要添加它们)


And to clear up what you initially thought. 并清理你最初的想法。 If you didn't have the first <tr> tag, then the following would work... 如果您没有第一个<tr>标签,那么以下内容将起作用......

@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. 注意整个if语句行不需要@符号,因为我们从未移回到HTML模式。

The problem with your code is that you mix up what's HTML and what's Razor. 你的代码的问题在于你混淆了什么是HTML和什么是Razor。 The @ sign is supposed to be where your Razor code starts and then you don't need to use it inside of the Razor code. @符号应该是你的Razor代码启动的地方,然后你不需要在Razor代码中使用它。

When you write if(@userMessage.Message.MessageText.Length <= 10) then if is not seen as Razor code but part of the HTML. 当你写if(@userMessage.Message.MessageText.Length <= 10) if不被视为Razor代码而是HTML的一部分。 So everytime you leave the HTML and go into Razor mode you need to have a @ mark. 所以每当你离开HTML并进入Razor模式时,你需要有一个@标记。 The if statement should instead look like @if(userMessage.Message.MessageText.Length <= 10) . if语句应该看起来像@if(userMessage.Message.MessageText.Length <= 10)

A complete example of your code would look like: 代码的完整示例如下所示:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM