jQuery的使用在asp.net mvc的4 _Layout.cshtml [英] jquery usage in asp.net mvc 4 _Layout.cshtml

查看:199
本文介绍了jQuery的使用在asp.net mvc的4 _Layout.cshtml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net的MVC 4项目中使用jQuery的。 jQuery的code为_layout,CSHTML。在IE 9,抛出异常。

 在第14行未处理的异常,在HTTP栏9://本地主机:59899 /
0x800a1391 - 微软JScript运行时错误:'jQuery的'未定义

在Firefox中,没有错误被抛出,但jQuery的没有在所有的工作。

 <!DOCTYPE HTML>
< HTML LANG =ENGT&;
< HEAD>
   <间的charset =UTF-8/>
   <标题> @ ViewBag.Title< /标题>
   <链接HREF =〜/ favicon.ico的相对=快捷方式图标类型=图像/ X-图标/>
   < META NAME =视口CONTENT =WIDTH =设备宽度/>
   @ Styles.Render(〜/内容/ CSS)
   @ Scripts.Render(〜/包/ Modernizr的)<脚本类型=文/ JavaScript的>
    jQuery的(文件)。就绪(函数(){
        变种divone = jQuery的(主内容)的高度();
        VAR divtwo =的jQuery(侧边栏)高度()。
        VAR maxdiv = Math.max(divone,divtwo);
        jQuery的(主要内容)高度(maxdiv)。
        。jQuery的(文)的高度(maxdiv);
    });
< / SCRIPT>

更新:

在App_Start有一个BundleConfig.cs。

 公共类BundleConfig
   {
    //关于捆绑的更多信息,请访问http://go.microsoft.com/fwlink/?LinkId=254725
    公共静态无效RegisterBundles(BundleCollection包)
    {
        bundles.Add(新ScriptBundle(〜/包/ jQuery的)。包括(
                    〜/脚本/ jquery- {}版本的.js));        bundles.Add(新ScriptBundle(〜/包/ jQueryUI的)。包括(
                    〜/脚本/ jQuery的 - 用户界面 - {}版本的.js));        bundles.Add(新ScriptBundle(〜/包/ jqueryval)。包括(
                    〜/脚本/ jquery.unobtrusive *,
                    〜/脚本/ jquery.validate *));        //使用Modernizr的开发版本,开发和借鉴。然后,当你
        //为生产做好准备,在使用的http://modernizr.com构建工具挑选您所需要的测试。
        bundles.Add(新ScriptBundle(〜/包/ Modernizr的)。包括(
                    〜/脚本/ modernizr- *));        bundles.Add(新StyleBundle(〜/内容/ CSS),包括(〜/内容/的site.css));        bundles.Add(新StyleBundle(〜/内容/主题/基/ CSS)。包括(
                    〜/内容/主题/基/ jquery.ui.core.css
                    〜/内容/主题/基/ jquery.ui.resizable.css
                    〜/内容/主题/基/ jquery.ui.selectable.css
                    〜/内容/主题/基/ jquery.ui.accordion.css
                    〜/内容/主题/基/ jquery.ui.autocomplete.css
                    〜/内容/主题/基/ jquery.ui.button.css
                    〜/内容/主题/基/ jquery.ui.dialog.css
                    〜/内容/主题/基/ jquery.ui.slider.css
                    〜/内容/主题/基/ jquery.ui.tabs.css
                    〜/内容/主题/基/ jquery.ui.datepicker.css
                    〜/内容/主题/基/ jquery.ui.progressbar.css
                    〜/内容/主题/基/ jquery.ui.theme.css));
    }
}


解决方案

请确保您包括在_Layout.cshtml捆绑

  @ Scripts.Render(〜/包/ jQuery的)
@ Scripts.Render(〜/包/ jQueryUI的)
@ Scripts.Render(〜/包/ jqueryval)

和确保束在BundleConfig.cs定义的:

  bundles.Add(新ScriptBundle(〜/包/ jQuery的)。包括(
            〜/脚本/ jQuery的-1.9.0.js
           〜/脚本/ jquery.unobtrusive-ajax.js));        bundles.Add(新ScriptBundle(〜/包/ jQueryUI的)。包括(
            〜/脚本/ jQuery的-UI-1.10.0.js
            〜/脚本/ jquery.ui.timepicker.js
            〜/脚本/ Ascende.Common.js
            〜/脚本/ jquery.contextMenu.js
            〜/脚本/ jquery.blockUI.js));        bundles.Add(新ScriptBundle(〜/包/ jqueryval)。包括(
            〜/脚本/ jquery.validate *));

请确保您使用的是组合中的正确的版本和文件名。
[1] <一个href=\"http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification\">http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification

在您的情况:

 &LT;!DOCTYPE HTML&GT;
 &LT; HTML LANG =ENGT&;
 &LT; HEAD&GT;
 &LT;间的charset =UTF-8/&GT;
 &LT;标题&GT; @ ViewBag.Title&LT; /标题&GT;
 &LT;链接HREF =〜/ favicon.ico的相对=快捷方式图标类型=图像/ X-图标/&GT;
 &LT; META NAME =视口CONTENT =WIDTH =设备宽度/&GT;
 @ Styles.Render(〜/内容/ CSS)
 @ Scripts.Render(〜/包/ Modernizr的)
 @ Scripts.Render(〜/包/ jQuery的)&LT;! - 添加此 - &GT;&LT;脚本类型=文/ JavaScript的&GT;
    jQuery的(文件)。就绪(函数(){
        变种divone = jQuery的(主内容)的高度();
        VAR divtwo =的jQuery(侧边栏)高度()。
        VAR maxdiv = Math.max(divone,divtwo);
        jQuery的(主要内容)高度(maxdiv)。
        。jQuery的(文)的高度(maxdiv);
    });
&LT; / SCRIPT&GT;

I used jquery in my asp.net mvc 4 project. The jquery code is in _Layout,cshtml. In IE 9, an exception was thrown.

Unhandled exception at line 14, column 9 in http://localhost:59899/
0x800a1391 - Microsoft JScript runtime error: 'jQuery' is undefined

In firefox, no error was thrown but jquery didn't work at all.

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="utf-8" />
   <title>@ViewBag.Title</title>
   <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
   <meta name="viewport" content="width=device-width" />
   @Styles.Render("~/Content/css")
   @Scripts.Render("~/bundles/modernizr")

<script type="text/javascript">
    jQuery(document).ready(function () {
        var divone = jQuery(".main-content").height();
        var divtwo = jQuery(".sidebar").height();
        var maxdiv = Math.max(divone, divtwo);
        jQuery(".main-content").height(maxdiv);
        jQuery(".sidebar").height(maxdiv);
    });
</script>

UPDATE:

In App_Start there is a BundleConfig.cs.

   public class BundleConfig
   {
    // For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkId=254725
    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
                    "~/Scripts/jquery-ui-{version}.js"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.unobtrusive*",
                    "~/Scripts/jquery.validate*"));

        // Use the development version of Modernizr to develop with and learn from. Then, when you're
        // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
        bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                    "~/Scripts/modernizr-*"));

        bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));

        bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
                    "~/Content/themes/base/jquery.ui.core.css",
                    "~/Content/themes/base/jquery.ui.resizable.css",
                    "~/Content/themes/base/jquery.ui.selectable.css",
                    "~/Content/themes/base/jquery.ui.accordion.css",
                    "~/Content/themes/base/jquery.ui.autocomplete.css",
                    "~/Content/themes/base/jquery.ui.button.css",
                    "~/Content/themes/base/jquery.ui.dialog.css",
                    "~/Content/themes/base/jquery.ui.slider.css",
                    "~/Content/themes/base/jquery.ui.tabs.css",
                    "~/Content/themes/base/jquery.ui.datepicker.css",
                    "~/Content/themes/base/jquery.ui.progressbar.css",
                    "~/Content/themes/base/jquery.ui.theme.css"));
    }
}

解决方案

Make sure you are including the bundle in the _Layout.cshtml

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryui")
@Scripts.Render("~/bundles/jqueryval")

And make sure the bundles are defined in your BundleConfig.cs:

 bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
            "~/Scripts/jquery-1.9.0.js",
           "~/Scripts/jquery.unobtrusive-ajax.js"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
            "~/Scripts/jquery-ui-1.10.0.js",
            "~/Scripts/jquery.ui.timepicker.js",
            "~/Scripts/Ascende.Common.js",
            "~/Scripts/jquery.contextMenu.js",
            "~/Scripts/jquery.blockUI.js"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
            "~/Scripts/jquery.validate*"));

Make sure you are using the correct version and file name in your bundle. [1] http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification

in your case:

 <!DOCTYPE html>
 <html lang="en">
 <head>
 <meta charset="utf-8" />
 <title>@ViewBag.Title</title>
 <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
 <meta name="viewport" content="width=device-width" />
 @Styles.Render("~/Content/css")
 @Scripts.Render("~/bundles/modernizr")
 @Scripts.Render("~/bundles/jquery") <!-- add this --> 

<script type="text/javascript">
    jQuery(document).ready(function () {
        var divone = jQuery(".main-content").height();
        var divtwo = jQuery(".sidebar").height();
        var maxdiv = Math.max(divone, divtwo);
        jQuery(".main-content").height(maxdiv);
        jQuery(".sidebar").height(maxdiv);
    });
</script>

这篇关于jQuery的使用在asp.net mvc的4 _Layout.cshtml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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