简体   繁体   English

混合Javascript和Razor代码

[英]Intermingle Javascript and Razor Code

I have the following Jquery Slider initializer that I want to add a simple comma after the end of a line that needs to be programatically populated. 我有以下Jquery Slider初始化程序,我想在需要以编程方式填充的行的末尾添加一个简单的逗号。

$(function () {
    $("#slider").slider({
        value: 1,
        min: 1,
        max: @Model.Offer.PurchaseLimit, <---problematic comma
        step: 1,
        slide: function (event, ui){
            $("#amount").val(ui.value);
        }
    });
});

The comma gets underlined as a 'syntax error'. 逗号下划线为“语法错误”。 My guess is that Razor is trying to interpret the comma. 我的猜测是Razor试图解释逗号。

I have also tried to create a variable outside the javascript function like this: 我也试图在javascript函数之外创建一个变量,如下所示:

var PurchaseMax = @Model.Offer.PurchaseLimit; <----problematic semi-colon

But the problem just becomes the semi-colon. 但是问题就变成了分号。 What do I do to get this to work? 我该怎么做才能使它正常工作?

do like this: 这样做:

max: @(Model.Offer.PurchaseLimit),

this way the scope of the razor expression is between @( and ) 这样剃刀表达式的范围就在@()之间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM