foreach 内有条件,Razor 内有条件

发布于 2024-12-27 23:57:38 字数 617 浏览 2 评论 0原文

我正在尝试学习 Razor,但遇到了语法问题。当我运行以下代码时:

@if (searchTerm != ""){
  <h2>Showing @ExamineManager.Instance.Search(searchTerm, true).Count() results for @searchTerm</h2>
  <div class="search-results">
    @foreach (var result in ExamineManager.Instance.Search(searchTerm, true)) {   
      if (result.Fields["nodeTypeAlias"] == 'File'){
        <p>File</p>
      }else{
        <p>Not file</p>  
      }
    }
  </div>
}

出现此错误,提示“字符文字中的字符过多”。代码的实际机制是有效的,我认为这只是条件嵌套方式的语法问题,但我尝试了@{}块的各种组合并将@添加到各个行,但就是无法让它工作。

谁能看到我做错了什么吗?

谢谢!

I'm trying to learn Razor but have come up against a syntax problem. When I run the following code:

@if (searchTerm != ""){
  <h2>Showing @ExamineManager.Instance.Search(searchTerm, true).Count() results for @searchTerm</h2>
  <div class="search-results">
    @foreach (var result in ExamineManager.Instance.Search(searchTerm, true)) {   
      if (result.Fields["nodeTypeAlias"] == 'File'){
        <p>File</p>
      }else{
        <p>Not file</p>  
      }
    }
  </div>
}

this errors, saying "Too many characters in character literal". The actual mechanism of the code works, it's just a syntax problem with the way the conditionals are nested I think, but I've tried various combinations of @{} blocks and prepending @ to various lines but just can't get it to work.

Can anyone see what I'm doing wrong?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葬﹪忆之殇 2025-01-03 23:57:38

'File' 语法无效。当您想要使用字符串时,您可能在 if 条件中指的是 "File"

if (result.Fields["nodeTypeAlias"] == "File")

'File' is invalid syntax. You probably meant "File" in your if condition when you want to work with strings:

if (result.Fields["nodeTypeAlias"] == "File")
墨小墨 2025-01-03 23:57:38

那是因为你在 c# 中进行了比较

result.Fields["nodeTypeAlias"] == 'File'

,你不能像那样将字符串写入单引号中。你应该把它改成

result.Fields["nodeTypeAlias"] == "File"

That is because you've got comparison

result.Fields["nodeTypeAlias"] == 'File'

In c#, you cannot write strings into single quotes like that. You should change it into

result.Fields["nodeTypeAlias"] == "File"
撩动你心 2025-01-03 23:57:38

只需更改代码,这样

    @{

      if (searchTerm != ""){
      <h2>Showing @ExamineManager.Instance.Search(searchTerm, true).Count() results for @searchTerm</h2>
      <div class="search-results">
        foreach (var result in ExamineManager.Instance.Search(searchTerm, true)) {   
          if (result.Fields["nodeTypeAlias"] == 'File'){
            <p>File</p>
          }
          else{
            <p>Not file</p>  
          }
        }
      </div>
    }


        }

此代码将帮助您

Just change the Code In this way

    @{

      if (searchTerm != ""){
      <h2>Showing @ExamineManager.Instance.Search(searchTerm, true).Count() results for @searchTerm</h2>
      <div class="search-results">
        foreach (var result in ExamineManager.Instance.Search(searchTerm, true)) {   
          if (result.Fields["nodeTypeAlias"] == 'File'){
            <p>File</p>
          }
          else{
            <p>Not file</p>  
          }
        }
      </div>
    }


        }

THis code will help you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文