最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net mvc - Syntax error in Razor view when passing model properties as parameters to javascript function - Stack Overflow

programmeradmin2浏览0评论

I get a Syntax error on each ma (,) and on the last bracket ()) in the following code:

<script type="text/javascript">
    Filters.priceRangeInit(@Model.Min, @Model.Max, @Model.From, @Model.To);
</script>

The javascript function is in another file and looks like this:

Filters = {
    priceRangeInit: function (min, max, from, to) {
        $('#price-range-slider').slider({
            min: min,
            max: max,
            step: 50,
            values: [from, to],
            slide: function (event, ui) {
                $('#left-price-range-amount').val(ui.values[0]);
                $('#right-price-range-amount').val(ui.values[1]);
            }
        });
    }
};

The model looks like this:

public class PriceRangeFilterModel
{
    public int Min { get; set; }
    public int Max { get; set; }
    public int From { get; set; }
    public int To { get; set; }
}

I use using Visual Studio 2012, ASP.NET MVC 3 and Razor and everything works fine in the browser, but I want to get rid of the 4 syntax errors I get in the IDE.

If I pass the parameters as strings I don't get errors anymore, but then I'd have to cast each one back to int to make the slider work:

<script type="text/javascript">
    Filters.priceRangeInit('@Model.Min', '@Model.Max', '@Model.From', '@Model.To');
</script>

Do you have any other ideas about how to write this call so that I don't get syntax errors?

I get a Syntax error on each ma (,) and on the last bracket ()) in the following code:

<script type="text/javascript">
    Filters.priceRangeInit(@Model.Min, @Model.Max, @Model.From, @Model.To);
</script>

The javascript function is in another file and looks like this:

Filters = {
    priceRangeInit: function (min, max, from, to) {
        $('#price-range-slider').slider({
            min: min,
            max: max,
            step: 50,
            values: [from, to],
            slide: function (event, ui) {
                $('#left-price-range-amount').val(ui.values[0]);
                $('#right-price-range-amount').val(ui.values[1]);
            }
        });
    }
};

The model looks like this:

public class PriceRangeFilterModel
{
    public int Min { get; set; }
    public int Max { get; set; }
    public int From { get; set; }
    public int To { get; set; }
}

I use using Visual Studio 2012, ASP.NET MVC 3 and Razor and everything works fine in the browser, but I want to get rid of the 4 syntax errors I get in the IDE.

If I pass the parameters as strings I don't get errors anymore, but then I'd have to cast each one back to int to make the slider work:

<script type="text/javascript">
    Filters.priceRangeInit('@Model.Min', '@Model.Max', '@Model.From', '@Model.To');
</script>

Do you have any other ideas about how to write this call so that I don't get syntax errors?

Share Improve this question asked Sep 23, 2012 at 17:23 david.sdavid.s 11.4k6 gold badges52 silver badges83 bronze badges 1
  • 2 Your code looks fine and because it works in the browser you should not care about the syntax error in VS. The visual studio intelisense - especially in razor - is not perfect. So just ignore the errors. – nemesv Commented Sep 23, 2012 at 18:29
Add a ment  | 

2 Answers 2

Reset to default 14

Razor Intellisense is just broken in Visual Studio. Your code is perfectly valid and you should ignore those warnings and hope that Microsoft will fix it in future versions.

To suppress the syntax errors, either wrap string values in quotes or wrap numbers in parseInt() as follows:

var myInt = parseInt('@Model.MyInt');
var myStr = '@Model.MyString';

Or, for your example:

Filters.priceRangeInit(parseInt('@Model.Min'), parseInt('@Model.Max'), parseInt('@Model.From'), parseInt('@Model.To'));

The roundtrip conversion is annoying but takes no time, really.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论