淘汰赛MVC4项目不工作 [英] knockout not working in MVC4 project

查看:134
本文介绍了淘汰赛MVC4项目不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Index.cshtml文件中的以下(从淘汰赛网站):

 < P>首先名称:其中,输入数据绑定=值:名字/>< / P>
< P>姓氏:其中,输入数据绑定=值:名字/>< / P>
< H2>您好,LT&;跨度数据绑定=TEXT:全名> < / SPAN>!< / H><脚本类型=文/ JavaScript的>
    //这是我的数据模型
    VAR视图模型=功能(第一,最后){
        this.firstName = ko.observable(第一);
        this.lastName = ko.observable(最后);        this.fullName = ko.computed(函数(){
            //淘汰赛自动跟踪依赖关系。它知道全名取决于firstName和lastName,因为评估全名,当这些被调用。
            返回this.firstName()++ this.lastName();
        }, 这个);
    };    ko.applyBindings(新视图模型(地球,地球)); //这使得淘汰赛开始工作
< / SCRIPT>

我的布局有这一行:

  @ Scripts.Render(〜/包/基因敲除)

这是打捆配置正确配置:

  bundles.Add(新ScriptBundle(〜/包/基因敲除)。包括(
                〜/脚本/库/淘汰赛2.2.1.js));

浏览器看到的文件,VS是给我的智能感知,所以我不知道发生了什么事。淘汰赛功能都在工作。

我测试了MVC的外(只使用HTML / CSS),它工作得很好。任何想法是怎么回事?

编辑:我试图用没有打捆的直接引用,它仍然不工作:

 <脚本类型=文/ JavaScript的SRC =〜/脚本/库/淘汰赛2.2.1.js>< / SCRIPT>

我是从铬得到一个错误:

 未捕获的Ref​​erenceError:劫没有定义


解决方案

你能不能从Chrome或萤火发布一些JS控制台错误?看起来像淘汰赛丢失的引用。

编辑:把你的脚本,在其视图的尽头!另一件事,淘汰赛参考必须保持的jQuery低于参考,因为它依赖。

编辑:
注册淘汰赛

  bundles.Add(新ScriptBundle(〜/包/基因敲除)。包括(
                    〜/脚本/ knockout- {}版本的.js));

呈现在您的视图

  @ Scripts.Render(〜/包/ jQuery的)
@ Scripts.Render(〜/包/基因敲除)

I have the following in my Index.cshtml file (from the knockout site):

<p>First name: <input data-bind="value: firstName" /></p>
<p>Last name: <input data-bind="value: lastName" /></p>
<h2>Hello, <span data-bind="text: fullName"> </span>!</h2>

<script type="text/javascript">
    // Here's my data model
    var ViewModel = function (first, last) {
        this.firstName = ko.observable(first);
        this.lastName = ko.observable(last);

        this.fullName = ko.computed(function () {
            // Knockout tracks dependencies automatically. It knows that fullName depends on firstName and lastName, because these get called when evaluating fullName.
            return this.firstName() + " " + this.lastName();
        }, this);
    };

    ko.applyBindings(new ViewModel("Planet", "Earth")); // This makes Knockout get to work
</script>

My Layout has this line:

@Scripts.Render("~/bundles/knockout")

Which is configured correctly in bundler config:

bundles.Add(new ScriptBundle("~/bundles/knockout").Include(
                "~/Scripts/Libraries/knockout-2.2.1.js"));

Chrome sees the file and VS is giving me intellisense, so I'm not sure what's going on. None of the knockout functions are working.

I tested this outside of MVC (just using html/css) and it worked fine. Any idea what's going on?

EDIT: I tried using a direct reference without bundler and it still doesn't work:

<script type="text/javascript" src="~/Scripts/Libraries/knockout-2.2.1.js"></script>

I'm getting an error from chrome:

Uncaught ReferenceError: ko is not defined 

解决方案

Could you post some JS console error from chrome or firebug? Looks like a missing reference from knockout.

EDIT: Put your script at the end of its view! Another thing, the reference for knockout must stay below the reference for jquery, because its dependent.

EDIT: Register the knockout

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

Render It in your view

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

这篇关于淘汰赛MVC4项目不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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