Asp.net mvc 3 当我遵循这个示例时,为什么会出现解析器错误?
我在这里遵循了这些指示: 注意:我使用的是网络矩阵 这个例子说要做到这一点:Asp.net mvc 3 当我遵循这个示例时,为什么会出现解析器错误?,asp.net-mvc-3,razor,Asp.net Mvc 3,Razor,我在这里遵循了这些指示: 注意:我使用的是网络矩阵 这个例子说要做到这一点: @ { var imagePath; if( Request["Choice"] != null) {imagePath="images\" + Request["Choice"];} } <!DOCTYPE html> <html> <body> <h1>Display Images</h1> <form method="post" ac
@
{
var imagePath;
if( Request["Choice"] != null)
{imagePath="images\" + Request["Choice"];}
}
<!DOCTYPE html>
<html>
<body>
<h1>Display Images</h1>
<form method="post" action="">
<div>
I want to see:
<select name="Choice">
<option value="Photo1.jpg">Photo 1</option>
<option value="Photo2.jpg">Photo 2</option>
<option value="Photo3.jpg">Photo 3</option>
</select>
<input type="submit" value="Submit" />
</div>
<div style="padding:10px;">
@if(imagePath != "")
{<img src="@imagePath" alt="Sample" />}
</div>
</form>
</body>
</html>
如果我将它们放在同一行上,则会出现以下错误:
Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: The code block is missing a closing "}" character. Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup.
Source Error:
Line 1: @{
Line 2: var imagePath;
Line 3: if( Request["Choice"] != null)
Source File: /Page.cshtml Line: 1
我不知道出了什么问题。您需要删除@character之后的换行符。更改代码如下
@{
var imagePath;
////other things..
这里有一个很好的语法参考
你也需要更改这些行
@{if(imagePath != "")
<img src="@imagePath" alt="Sample" />
}
@{if(imagePath!=“”)
}
以下是如何修复代码中的两个错误:
@{
var imagePath = "";
if(Request["Choice"] != null) {
imagePath = "images/" + Request["Choice"];
}
}
<!DOCTYPE html>
<html>
<body>
<h1>Display Images</h1>
<form method="post" action="">
<div>
I want to see:
<select name="Choice">
<option value="Photo1.jpg">Photo 1</option>
<option value="Photo2.jpg">Photo 2</option>
<option value="Photo3.jpg">Photo 3</option>
</select>
<input type="submit" value="Submit" />
</div>
<div style="padding:10px;">
@if(imagePath != "") {
<img src="@imagePath" alt="Sample" />
}
</div>
</form>
</body>
</html>
@{
var imagePath=“”;
if(请求[“选择”]!=null){
imagePath=“images/”+请求[“选择”];
}
}
显示图像
我想看:
照片1
照片2
照片3
@如果(imagePath!=“”){
}
您的代码在var imagePath处出错代码>
如果不指定值,则不能使用var
进行声明。
只需将其更改为:
var-imagePath=”“代码>
或
字符串图像路径代码>有些人只需将jquery.com中的代码示例复制到razor文件中,就会偶然发现这个答案。例如,我测试了一些东西,从一个长javascript变量值(一个正则表达式)中得到了错误
emailRegex = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,
注意@,我不得不添加第二个@来转义。。。。否则我会得到上面的错误,因此谷歌搜索会在顶部结果中显示这个stackoverflow页面。因此,虽然这不能直接回答问题,但我确信它可以帮助其他人。这也会返回一个错误,请参阅我刚才发布的错误。好的,这很有效,谢谢。。我猜w3schools.com的人有错误的代码?在他们的例子中是有效的,但在这里不是吗?这是一个必须完成的设置,才能像url中的示例那样工作吗?@Jeremy,W3Schools的示例是错误的。这不应该让你感到惊讶。这个网站不是我学习的最佳信息来源:-)这只是发生在我身上!谢谢
emailRegex = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,