2014-09-04 122 views
6

我正在处理MVC4应用程序(Razor引擎)。当过我尝试从一个按钮调用一个js函数,它说错误: -未终止字符串常量MVC4剃刀视图引擎

Unterminated string constant

enter image description here

但是它不给任何compile timeruntime错误和工作正常。

但在观点有它清楚地说,关于上述的错误:

为什么会这样?

+0

什么的Visual Studio版本 – haim770 2014-09-04 07:07:02

+0

的Visual Studio 2012 – Pawan 2014-09-04 07:07:33

回答

4

Razor解析器很挑剔,在某些情况下,它不能正确解析或对语法进行颜色编码。你的代码将起作用。

要解决这个问题,您可以用解析器知道如何解决这个问题的方式来格式化代码。使用括号周围的所有刀片服务器端代码引用:

<input type="button" id="btntest" value="Details" 
    onclick="ABC('@(Item.TourApplicationID)', '@(Item.TourID)')" /> 

编辑:上面可能只在VS 2013已经工作,有或没有更新。 (或者根本不清一色我只记得它的工作

下面应该工作,从MVC3 unterminated string constant syntax error采取:。

<input type="button" id="btntest" value="Details" 
    onclick="@("ABC('" & Item.TourApplicationID & "', '" & Item.TourID & "')")" /> 

(我认为这是正确的很难说没有颜色编码)

我喜欢的String.Format,这样的onclick看起来更像:

onclick="@(String.Format("ABC('{0}', '{1}')", Item.TourApplicationID, Item.TourID))" 
+0

好吧,让我来试试.. :) – Pawan 2014-09-04 07:09:55

+0

错误不会消失。它仍然会说相同的错误.. :( – Pawan 2014-09-04 07:11:37

+0

也许这是他们在VS 2013中更新的一个更新。 – ps2goat 2014-09-04 07:12:31