“$”是不确定的。如何使用jQuery 2.0.1空ASP.NET MVC 4项目? [英] '$' is undefined. How to use jQuery 2.0.1 in empty ASP.NET MVC 4 project?

查看:174
本文介绍了“$”是不确定的。如何使用jQuery 2.0.1空ASP.NET MVC 4项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经来看看:'$'是未定义,但它并没有解决我的问题; that's为什么我打开另一个线程。

I already take a look at: '$' is undefined, but it did not solve my problem; that´s why I open another thread.

这是我做过什么......

This is what I did...

我创建使用Visual Studio 2012新的解决方案,并添加一个空的ASP.NET MVC 4项目。我添加了一个控制器类,定义了一个默认的索引操作方法,并让ReSharper的为它创建的视图。该项目的结构如下:

I created a new solution using Visual Studio 2012 and added an empty ASP.NET MVC 4 project. I added a controller class, defined a default Index action method and let Resharper create the view for it. The project has the following structure:

root/
    Controllers/
        SampleController.cs
    Models/
    Views/
        Sample/
            Index.cshtml
        Shared/
            Layout.cshtml
    Scripts/
        jquery-2.0.1.js
        jquery-2-0.1.min.js
        jquery-2.0.1-min.map

的jQuery已经通过的NuGet增加;所以我手动添加一个BundleConfig.cs文件到文件夹App_Start并添加code定义jQuery脚本包(RegisterBundles将在Global.asax调用)。

jQuery has been added via Nuget; so I manually added a BundleConfig.cs file to the App_Start folder and added code to define the jQuery script bundle (RegisterBundles will be called by the global.asax).

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(new ScriptBundle("~/bundles/jquery")
        .Include("~/Scripts/jquery-2.0.*"));

}

我还定义具有以下内容的布局页...

I also defined a layout page that has the following content...

<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>@this.ViewBag.Title</title>
    </head>
    <body>
        <div>
            @this.RenderBody()
        </div>
        @this.RenderSection("Scripts", required: false)
    </body>
</html>

我的索引视图有以下内容...

My index view has the following content...

@using System.Web.Optimization
@model dynamic

@{
    this.Layout = "~/Views/Shared/Layout.cshtml";
}

@section Scripts
{
    @Scripts.Render("~/bundles/jquery")

    <script type="text/javascript">
        $(document).ready(function() {

        });
    </script>
}

下面的标记被渲染到输出流...

The following markup is rendered to the output stream...

<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="width=device-width" />
    </head>
    <body>
        <div>

        </div>

    <script src="/root/Scripts/jquery-2.0.1.js"></script>
    <script src="/root/Scripts/jquery-2.0.1.min.map"></script>

    <script type="text/javascript">
        $(document).ready(function() {

        });
    </script>

    </body>
</html>

当我调试应用程序它给我的错误消息$是不确定的。相反,我的托管本地IIS中的应用程序,我也试图通过视觉Studio's Web开发服务器来运行它,这给了我一个提示:一个语法错误(意外的标记';'的min.map文件中)。可能是为什么jQuery的未初始化的原因...但我应该怎么解决这个问题?

When I debug the application it gives me the error message '$' is undefined. Instead of hosting the application within my local IIS, I also tried to run it through Visual Studio´s Web Development Server, which gives me another hint: a syntax error (unexpected token ';' within the min.map file). Might be the reason why jQuery is not initialized... but how should I solve this?

推荐答案

实际的问题是有关我的本地IIS,其中的静态内容的功能不可用。这不得不说凡与确定200回答对Java和脚本 - 文件CSS-要求的效果,但与零内容长度。参见:<一href=\"http://serverfault.com/questions/115099/iis-content-length-0-for-css-javascript-and-images\">http://serverfault.com/questions/115099/iis-content-length-0-for-css-javascript-and-images关于如何解决IIS安装信息。

The actual problem was related to my local IIS, where the static content feature was not available. This had the effect that requests for java script- and css- files where answered with OK 200, but with zero content length. See: http://serverfault.com/questions/115099/iis-content-length-0-for-css-javascript-and-images for information on how to fix the IIS installation.

以下code现在按预期工作。随之改变的唯一事情是脚本包定义...

The following code works now as expected. The one and only thing that is changed is the script bundle definition...

BundleConfig.cs

BundleConfig.cs

namespace MvcApplication1
{
    using System.Web.Optimization;

    public class BundleConfig
    {
        public const string BundlesJquery = "~/bundles/jquery";

        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle(BundlesJquery)
                .Include("~/Scripts/jquery-{version}.js"));
        }
    }
}

Index.cshtml

Index.cshtml

@using System.Web.Optimization
@using MvcApplication1
@model dynamic

@{
    this.ViewBag.Title = "title";
    this.Layout = "~/Views/Shared/Layout.cshtml";
}

@section Scripts
{
    @Scripts.Render(BundleConfig.BundlesJquery)
    <script type="text/javascript">
        $(document).ready(function() {
            alert("Hello World.");
        })
    </script>    
}

Layout.cshtml

Layout.cshtml

@using System.Web.Optimization
@using MvcApplication1
<!DOCTYPE html>

<html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>@this.ViewBag.Title</title>
    </head>
    <body>
        <div>
            @this.RenderBody()
        </div>
        @this.RenderSection("Scripts", false)
    </body>
</html>

这篇关于“$”是不确定的。如何使用jQuery 2.0.1空ASP.NET MVC 4项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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