MVC4剃刀感到困惑括号 [英] MVC4 Razor confused about braces

查看:124
本文介绍了MVC4剃刀感到困惑括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了所有专家剃刀一个相当简单的问题在那里。我试图做一个jQuery $。阿贾克斯()调用到一个URL,使用Url.Content()翻译的家庭相对路径到根目录相对路径。这样做,剃须刀是有点困惑在我的@section年底的位置。我想preFER要能够指定URL内联,但是当我这样做,剃刀认为,$。阿贾克斯()的参数列表的末尾是我@section的结束。我使用@section因为我想用布局来把我的javascript在每个文件的底部。为什么剃刀变得如此混乱?我甚至使用@(Url.Content(...))试过了,但是这并不工作。

此外,这是解决这个问题的最好方法?我使用的是ASP.NET MVC 4 preVIEW。

这工作:

  @section脚本{
    <脚本类型=文/ JavaScript的>
        VAR getMessagesUrl ='@ Url.Content(〜/日志/的getMessages)';        $(文件)。就绪(函数(){
            $阿贾克斯({
                网址:getMessagesUrl,
                数据类型:HTML,
                成功:函数(结果){
                    $('TBODY')HTML(结果);
                }
            });
        });
    < / SCRIPT>
}

这不:

  @section脚本{
    <脚本类型=文/ JavaScript的>
        $(文件)。就绪(函数(){
            $阿贾克斯({
                网址:'@ Url.Content(〜/日志/的getMessages)',
                数据类型:HTML,
                成功:函数(结果){
                    $('TBODY')HTML(结果);
                }
            }); //剃刀认为这是大括号的结束部分!
        });
    < / SCRIPT>
}


解决方案

感谢所有的帮助下,乡亲们。

看起来它一定是在ASP.NET MVC 4 preVIEW的错误。我刚刚升级到出来2月15日ASP.NET MVC 4 Beta版,而现在的问题是完全消失了。

I've got a fairly simple question for all the Razor experts out there. I'm trying to make a jQuery $.ajax() call to a URL, using Url.Content() to translate the home-relative path into an root-relative path. In so doing, Razor is getting a bit confused about where the end of my @section is located. I'd prefer to be able to specify the URL inline, but when I do that, Razor thinks that the end of the $.ajax() parameter list is the end of my @section. I'm using @section because I want to use layouts to place my javascript at the bottom of each file. Why is Razor getting so confused? I've even tried using @(Url.Content(...)), but that doesn't work either.

Also, is this the best way to approach the problem? I'm using the ASP.NET MVC 4 Preview.

This works:

@section Scripts {
    <script type="text/javascript">        
        var getMessagesUrl = '@Url.Content("~/Logging/GetMessages")';

        $(document).ready(function () {
            $.ajax({
                url: getMessagesUrl,
                dataType: 'html',
                success: function (result) {
                    $('tbody').html(result);
                }
            });
        });
    </script>
}

This doesn't:

@section Scripts {
    <script type="text/javascript">        
        $(document).ready(function () {
            $.ajax({
                url: '@Url.Content("~/Logging/GetMessages")',
                dataType: 'html',
                success: function (result) {
                    $('tbody').html(result);
                }
            }); //Razor thinks this is the curly-brace that ends the section!
        });
    </script>
}

解决方案

Thanks for all of the help, folks.

It looks like it must have been a bug in the ASP.NET MVC 4 Preview. I just upgraded to the ASP.NET MVC 4 Beta which came out on February 15, and the problem is now completely gone.

这篇关于MVC4剃刀感到困惑括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆