2011-08-31 89 views
2

我这里按照这些指示:http://w3schools.com/razor/razor_example.asp 注:我使用Web Matrix的为什么当我遵循这个例子时会出现解析器错误?

的例子说,要做到这一点:

@ 
{ 
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> 
    &nbsp; 
    <input type="submit" value="Submit" /> 
</div> 
<div style="padding:10px;"> 
@if(imagePath != "") 
{<img src="@imagePath" alt="Sample" />} 
</div> 
</form> 
</body> 
</html> 

和所有我得到这个这个错误:

做任何事情必须设置为接受@ {正在分开行?

Server Error in '/' Application. 
    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: A space or line break was encountered after 
    the "@" character. Only valid identifiers, keywords, comments, 
    "(" and "{" are valid at the start of a code block and they must 
    occur immediately following "@" with no space in between. 


    Source Error: 


    Line 1: @ 
    Line 2: { 
    Line 3: var imagePath; 


    Source File: /Page.cshtml Line: 1 


    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1 

如果我把它们放在同一行,我得到这个错误:

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 

我不知道什么是错。

回答

2

这里是如何解决这对夫妻的错误在你的代码:

@{ 
    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> 
    &nbsp; 
    <input type="submit" value="Submit" /> 
</div> 
<div style="padding:10px;"> 
    @if(imagePath != "") { 
     <img src="@imagePath" alt="Sample" /> 
    } 
</div> 
</form> 
</body> 
</html> 
+0

好吧,这工作,谢谢..我想在w3schools.com的人有错误的代码?它在他们的例子中工作,但不在这里?这是一个必须做的工作,像在url中的例子一样工作吗? – Wilkins

+0

@Jeremy,W3Schools的例子是错误的。这不应该让你感到惊讶。这个网站不是我想要学习的最好的信息来源:-) –

2

你需要后@删除换行符character.change你像这样的代码

@{ 
    var imagePath; 
////other things.. 

这里是你http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx 一个良好的语法参考,您需要更改这些行也

@{if(imagePath != "") 

<img src="@imagePath" alt="Sample" /> 

} 
+0

也返回一个错误,看到我刚才发布的错误。 – Wilkins

+0

请参阅我已更新回答 –

1

您的代码在var imagePath;
错误您不能声明与var而不分配一个值。
它只是更改为:
var imagePath = "";

string imagePath;

5

有些人会碰到这个答案绊倒,只需将代码复制样本jquery.com到剃刀文件。例如,我正在测试一些东西,特别是从一个很长的javascript变量值中得到了一个正则表达式的错误。

emailRegex = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-][email protected]@[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])?)*$/, 

通知的@@,我不得不添加第二个@逃跑....否则我得到的错误之上,因此谷歌搜索显示我在上面结果此计算器页面。所以虽然这并不能直接回答这个问题,但我确信它可以帮助别人。

+0

很高兴知道,谢谢! – JosephDoggie

+0

这只是发生在我身上!谢谢。 –

相关问题