foreach 内有条件,Razor 内有条件
我正在尝试学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
'File'
语法无效。当您想要使用字符串时,您可能在if
条件中指的是"File"
:'File'
is invalid syntax. You probably meant"File"
in yourif
condition when you want to work with strings:那是因为你在 c# 中进行了比较
,你不能像那样将字符串写入单引号中。你应该把它改成
That is because you've got comparison
In c#, you cannot write strings into single quotes like that. You should change it into
只需更改代码,这样
此代码将帮助您
Just change the Code In this way
THis code will help you