简体   繁体   English

包含外部JavaScript的ASP.NET MVC 4问题

[英]ASP.NET MVC 4 issues with including external javascript

I'm new in MVC and i got stuck with using a downloaded javascript controller in a partial view. 我是MVC的新手,并且在部分视图中陷入了使用下载的javascript控制器的困境。 I downloaded this treeview: 我下载了这个树形视图:
http://bassistance.de/jquery-plugins/jquery-plugin-treeview/ http://bassistance.de/jquery-plugins/jquery-plugin-treeview/
Then I placed the treeview.js and demo.js scripts in the project, with the css and the images folder. 然后,我将css和images文件夹中的treeview.js和demo.js脚本放置在项目中。 The 2 .js files are in the /Scripts/treeview folder and the css and images folder is in the /Content folder. 2个.js文件位于/ Scripts / treeview文件夹中,而css和images文件夹位于/ Content文件夹中。

I added these to my BundleConfig.cs: 我将这些添加到我的BundleConfig.cs中:

        bundles.Add(new ScriptBundle("~/bundles/treeview").Include(
                    "~/Scripts/treeview/jquery.treeview.js",
                    "~/Scripts/treeview/demo.js"));

        bundles.Add(new StyleBundle("~/Content/css/treewiew").Include("~/Content/jquery.treeview.css"));

My partial view looks like this: 我的局部视图如下所示:

<ul id="browser" class="treeview">
    <li><span class="folder">Folder 1</span>
        <ul>
            <li><span class="file">Item 1.1</span></li>
        </ul>
    </li>
    <li><span class="folder">Folder 2</span>
        <ul>
            <li><span class="folder">Subfolder 2.1</span>
                <ul id="folder21">
                    <li><span class="file">File 2.1.1</span></li>
                    <li><span class="file">File 2.1.2</span></li>
                </ul>
            </li>
            <li><span class="file">File 2.2</span></li>
        </ul>
    </li>
    <li class="closed"><span class="folder">Folder 3 (closed at start)</span>
        <ul>
            <li><span class="file">File 3.1</span></li>
        </ul>
    </li>
    <li><span class="file">File 4</span></li>

In the _Layout.cshtml I added these 2 lines in the head section, just before the @Styles.Render("~/Content/css") and @Scripts.Render("~/bundles/modernizr") lines: 在_Layout.cshtml中,我在标题部分的@ Styles.Render(“〜/ Content / css”)和@ Scripts.Render(“〜/ bundles / modernizr”)行之前添加了这两行:

<link href="@Url.Content("~/Content/jquery.treeview.css")" rel="stylesheet" type="text/css" />
<script runat="server" type="text/javascript" src="@Url.Content("~/Scripts/treeview/jquery.treeview.js")"></script>

And lastly, I call the partial view in the Index view, which is generated automatically with the project. 最后,我在索引视图中调用局部视图,该视图随项目自动生成。 The css works almost fine, I've got the styled list, but it doesn't functional as a tree view controller. CSS几乎可以正常工作,我有样式列表,但它不能用作树视图控制器。

Now, I found these posts, but these weren't able to help me: 现在,我找到了这些帖子,但是这些不能帮助我:
How to include javascript code in asp.net MVC4 View page? 如何在asp.net MVC4“查看”页面中包含JavaScript代码?
MVC4 partial view javascript bundling Issue MVC4部分视图javascript捆绑问题

I just can't see what's the problem at me. 我只是看不到我有什么问题。 I've just started to learn javascript recently, MVC was totally new and unknown to me 5 days before, and I stuck with this problem for 3 days now and I'm angry at myself for that. 我最近才刚开始学习javascript,MVC在5天之前对我来说是一个全新的陌生人,而我现在坚持这个问题已有3天了,对此我感到很生气。 Please, help me! 请帮我! Thanks a lot! 非常感谢!

Add this line instead of using your script registration, 添加此行而不是使用脚本注册,

@Scripts.Render("~/bundles/jquery")    
@Scripts.Render("~/bundles/treeview")

Note: treeview should register after the jQuery reference. 注意: treeview应该在jQuery参考之后注册。

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

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