简体   繁体   English

在局部视图上无法识别出淘汰赛JS

[英]Knockout JS is not recognized on partial view

I have a main page which has some javascript, a partial view loaded from the main page, and a second partial view loaded from the partial view page. 我有一个包含一些javascript的主页,从主页加载的局部视图,以及从局部视图页面加载的第二局部视图。

The main page references a javascript source file, which includes knockout js which states a number of ko.observables. 主页上引用了一个javascript源文件,其中包括剔除js,其中指出了许多ko.observables。

These ko.observables work as intended for the main page, but they fail to recognize values of the partial view and second partial view, when the model is passed through. 这些ko.observables可以按预期用于主页的方式工作,但是当传递模型时,它们无法识别部分视图和第二部分视图的值。

Does the same javascript file really need to be referenced on every view? 是否真的需要在每个视图上引用相同的javascript文件?

You don't need to add reference to partial pages rather first load the partial views via ajax calls and then apply binding into the callback function like this example: 您无需添加对部分页面的引用,而是首先通过ajax调用加载部分视图,然后将绑定应用于回调函数,例如以下示例:

ASP.NET MVC Controller: ASP.NET MVC控制器:

public class TestController:Controller
{
  [HttpGet]
  public ActionResult MyView()
  {
    return PartialView("_MyView");
  }

}

HTML and Javascript : HTML和Javascript

    <div id="ContentHolder" ><div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script>

    $.get('Test/MyView', function(view){
        var _contentHolder = document.getElementById('ContentHolder');

       _contentHolder.html(view);

        ko.applyBinding(self, _contentHolder);
    })
    </script>

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

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