简体   繁体   English

ASP.NET MVC视图中的淘汰赛绑定

[英]Knockout binding in ASP.NET MVC view

In my application we are fetching a json data from the ASP.NET MVC method. 在我的应用程序中,我们正在从ASP.NET MVC方法中获取json数据。 And binding view using data-bind. 并使用数据绑定视图绑定。 At present I am using Knockout JavaScript library v2.2.1 and Knockout Mapping plugin v2.3.5. 目前,我正在使用Knockout JavaScript库v2.2.1和Knockout Mapping插件v2.3.5。

So whenever I load a partial view, I have to call same binding method again to bind properties in the partial view. 因此,无论何时加载局部视图,都必须再次调用相同的绑定方法来绑定局部视图中的属性。

So if I update knockout library to latest one and call binding method again it throws the following error: 因此,如果我将敲除库更新为最新的库,然后再次调用绑定方法,则会引发以下错误:

Uncaught Error: You cannot apply bindings multiple times to the same element. 未捕获的错误:您不能多次将绑定应用于同一元素。

Is there any way that we call binding method once and after that bind properties in the partial view without calling binding method? 有什么方法可以在局部视图中一次又一次在不调用绑定方法的情况下调用绑定方法? or what I have to change for using new library version? 还是使用新库版本需要更改的内容?

Method I am using at present to bind data and call every time when I load a partial view 我目前使用的方法是在每次加载局部视图时绑定数据并调用

 function getResourceFile(CallBack) { var Menu = function (data) { var self = this; ko.mapping.fromJS(data, {}, self); }; if (typeof localStorage === 'object') { try { // Geeting language and localize application on this behalf STARTED var lang = localStorage.getItem('lan'); var userLang = ''; if (lang === null || lang === 'undefined' || lang === '') { userLang = window.navigator.language || window.navigator.userLanguage; if (typeof userLang == 'undefined') userLang = "en"; } else { userLang = lang; } //userLang = "en";//comment this if (userLang.toString().length == 2) { if (userLang == 'de') { CurrentLocale = "de-DE"; } else { CurrentLocale = "en-US"; } } var l_lang = $.trim(userLang.substr(0, 2)); var currentURL = document.URL; if (currentURL.indexOf("SelectApp/de") != -1) { userLang = 'de'; CurrentLocale = "de-DE"; l_lang = 'de'; } else if (currentURL.indexOf("SelectApp/en") != -1) { userLang = 'en'; CurrentLocale = "en-US"; l_lang = 'en'; } var jsonName = endpoints.CPRes + l_lang; localStorage.setItem('lan', userLang); $.getJSON(jsonName, function (data) { LocalizationViewModel = data; ko.applyBindings(new Menu(data)); CallBack && CallBack(); }); } catch (e) { Storage.prototype._setItem = Storage.prototype.setItem; Storage.prototype.setItem = function () { }; alert('Your web browser does not support storing settings locally. In Safari, the most common cause of this is using "Private Browsing Mode". Some settings may not save or some features may not work properly for you.'); } } } function BindDataViewModel() { if (LocalizationViewModel === null || LocalizationViewModel === 'undefined' || LocalizationViewModel === '') { getResourceFile(); } else { //var localdeferred = $.Deferred(); var t = setTimeout(function () { ko.applyBindings(LocalizationViewModel); // localdeferred.resolve(); }, 300); //return localdeferred; } } 

Rather than binding to the entire screen, you can specify a DOM element to bind to as the second parameter of ko.applyBindings 您可以指定要绑定到的DOM元素作为ko.applyBindings的第二个参数,而不是绑定到整个屏幕

To avoid "Uncaught Error: You cannot apply bindings multiple times to the same element." 为避免出现“未捕获的错误:您不能多次将绑定应用于同一元素。” you can specify an element id on your partial for the data to bind to 您可以在局部上指定元素ID以将数据绑定到

ko.applyBindings(LocalizationViewModel, document.getElementById(elementId)

Where elementId is the unique id of an element on your partial view. 其中elementId是部分视图上元素的唯一ID。

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

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