它是可能的,或有一个工作区,以使用Razor语法在JavaScript中,是在一个视图(cshtml)?

我正在尝试添加标记到谷歌地图…例如,我尝试了这个,但我得到了大量的编译错误:

<script type="text/javascript">

    // Some JavaScript code here to display map, etc.

    // Now add markers
    @foreach (var item in Model) {

        var markerlatLng = new google.maps.LatLng(@(Model.Latitude), @(Model.Longitude));
        var title = '@(Model.Title)';
        var description = '@(Model.Description)';
        var contentString = '<h3>' + title + '</h3>' + '<p>' + description + '</p>'

        var infowindow = new google.maps.InfoWindow({
            content: contentString
        });

        var marker = new google.maps.Marker({
            position: latLng,
            title: title,
            map: map,
            draggable: false
        });

        google.maps.event.addListener(marker, 'click', function () {
            infowindow.open(map, marker);
        });
    }
</script>

当前回答

一个简单明了的例子:

<script>
    // This gets the username from the Razor engine and puts it
    // in JavaScript to create a variable I can access from the
    // client side.
    //
    // It's an odd workaraound, but it works.
    @{
        var outScript = "var razorUserName = " + "\"" + @User.Identity.Name + "\"";
    }
    @MvcHtmlString.Create(outScript);
</script>

这将在您的页面中创建一个脚本,在您放置上面的代码的位置,看起来如下所示:

<script>
    // This gets the username from the Razor engine and puts it
    // in JavaScript to create a variable I can access from
    // client side.
    //
    // It's an odd workaraound, but it works.

    var razorUserName = "daylight";
</script>

现在您有了一个全局JavaScript变量razorUserName,您可以在客户机上访问和使用它。Razor引擎显然已经从@User.Identity中提取了这个值。名称(服务器端变量),并将其放入它写入脚本标记的代码中。

其他回答

一个简单明了的例子:

<script>
    // This gets the username from the Razor engine and puts it
    // in JavaScript to create a variable I can access from the
    // client side.
    //
    // It's an odd workaraound, but it works.
    @{
        var outScript = "var razorUserName = " + "\"" + @User.Identity.Name + "\"";
    }
    @MvcHtmlString.Create(outScript);
</script>

这将在您的页面中创建一个脚本,在您放置上面的代码的位置,看起来如下所示:

<script>
    // This gets the username from the Razor engine and puts it
    // in JavaScript to create a variable I can access from
    // client side.
    //
    // It's an odd workaraound, but it works.

    var razorUserName = "daylight";
</script>

现在您有了一个全局JavaScript变量razorUserName,您可以在客户机上访问和使用它。Razor引擎显然已经从@User.Identity中提取了这个值。名称(服务器端变量),并将其放入它写入脚本标记的代码中。

我刚刚写了这个辅助函数。把它放在App_Code/JS.cshtml中:

@using System.Web.Script.Serialization
@helper Encode(object obj)
{
    @(new HtmlString(new JavaScriptSerializer().Serialize(obj)));
}

然后在你的例子中,你可以这样做:

var title = @JS.Encode(Model.Title);

注意我没有加引号。如果标题已经包含引号,它就不会爆炸。似乎处理字典和匿名对象也很好!

您看到了哪些具体的错误?

这样做会更好:

<script type="text/javascript">

//now add markers
 @foreach (var item in Model) {
    <text>
      var markerlatLng = new google.maps.LatLng(@Model.Latitude, @Model.Longitude);
      var title = '@(Model.Title)';
      var description = '@(Model.Description)';
      var contentString = '<h3>' + title + '</h3>' + '<p>' + description + '</p>'
    </text>
}
</script>

注意,foreach后面需要神奇的<text>标记来指示Razor应该切换到标记模式。

只要它在CSHTML页面中,而不是外部JavaScript文件中,就可以正常工作。

Razor模板引擎不关心输出什么,也不区分<script>或其他标记。

但是,您需要对字符串进行编码以防止XSS攻击。

有一件事要补充-我发现Razor语法分析器(可能是编译器)对开始括号的位置有不同的解释:

<script type="text/javascript">
    var somevar = new Array();

    @foreach (var item in items)
    {  // <----  placed on a separate line, NOT WORKING, HILIGHTS SYNTAX ERRORS
        <text>
        </text>
    }

    @foreach (var item in items) {  // <----  placed on the same line, WORKING !!!
        <text>
        </text>
    }
</script>