【问题标题】:Knockout Binding淘汰赛绑定
【发布时间】:2013-02-04 02:48:02
【问题描述】:

我正在通过 Knockout 将一些列表项绑定到下拉列表,但它没有绑定。我不知道我哪里错了..

我使用了敲除映射插件,甚至尝试了一个简单的方法,但似乎没有任何效果。

我的基本结构是这样的:

BugsReport rp = new BugsReport()
{
     SoftwareProductList = new List<SoftProduct>() { new SoftProduct() { ProductName = "eCommerce Website", SoftProId = 1 }, new SoftProduct() { ProductName = "Banking website", SoftProId = 2 } },
     ListBugs = GetAllBugs(),
     PriorityLevels = new List<Priority>() { new Priority() { PriorityId = 1, PriorityName = "P1" }, new Priority() { PriorityId = 2, PriorityName = "P2" }, new Priority() { PriorityId = 3, PriorityName = "P3" } }
};

我从控制器发送... 正常剃须刀绑定正在发生,但不是淘汰赛。

HTML部分

<div  style="margin-top: 10px; width: 200px; float: left; font-weight: bold;">
    Products
    <select id="slSoftProducts" multiple="multiple" data-bind="options: $root.ProductList, value:ProductList.SoftProId, optionsText: 'ProductList.ProductName'">. </select>
</div>
<div style="margin-top: 10px; width: 200px; float: left; font-weight: bold; margin-left: 30px;">
     priority Levels
     <select id="slPriorityLevels" multiple="multiple" data-bind="options: $root.priorityList, value: priorityList.PriorityId, optionsText: 'priorityList.PriorityName'"></select>
</div>

和 Javascript 部分

function bugzillaviewmodel(){
    var self = this;
    self.ProductList = BugList.SoftwareProductList;
    self.priorityList = BugList.PriorityLevels;                         
}     

var viewModel = new bugzillaviewmodel();

// Knock Out Binding   through mapping.. 
//var viewModel = ko.mapping.fromJS(BugList);    
ko.applyBindings(viewModel);   

【问题讨论】:

  • 请努力正确格式化和拼写您的问题。

标签: c# asp.net-mvc-3 knockout.js knockout-mapping-plugin knockout-2.0


【解决方案1】:

看起来您的绑定引用不正确,您将对象附加到所需属性的名称之前。试试这个:

        <div  style="margin-top: 10px; width: 200px; float: left; font-weight: bold;">
            Products
            <select id="slSoftProducts" multiple="multiple" data-bind="options: ProductList, value: SoftProId, optionsText: 'ProductName'"></select>
        </div>
        <div style="margin-top: 10px; width: 200px; float: left; font-weight: bold; margin-left: 30px;">
            priority Levels
          <select id="slPriorityLevels" multiple="multiple" data-bind="options: priorityList, value: PriorityId, optionsText: 'PriorityName'"></select>
        </div>

您还需要创建绑定到可观察对象的属性。查看mapping plugin 以使您的视图模型具有self.ProductListself.priorityList 可观察属性。

我还删除了 $root 引用,因为我认为你不需要它。

【讨论】:

  • 嗨 Itried 你的更改给了我脚本错误...Microsoft JScript 运行时错误:无法解析绑定。消息:TypeError:'SoftProId' 未定义;绑定值:选项:ProductList,值:SoftProId,optionsText:'ProductName'
  • 那么你需要使属性可观察,以便 Knockout 能够使用它们。
  • 为了使属性可观察,我使用了映射插件 viewModel = ko.mapping.fromJS(BugList);这会将视图模型转换为可观察的属性。仍然那么值没有约束力。
  • 你能创建一个 jsFiddle 来演示你的问题吗?
  • 这是我之前创建的一个:jsfiddle.net/gvBXh/2 您需要确保在左侧菜单的“管理资源”部分中添加 Knockout 和来自 CDN 的映射插件。
【解决方案2】:

SoftProId 和 PriorityId 没有在任何地方定义。如果它包含在 ProductList 对象中,那么它应该是这样的。

<select id="slSoftProducts" multiple="multiple" data-bind="options: ProductList.List, value: ProductList.SoftProId, optionsText: 'ProductName'"></select>

使用你的视图模型

function bugzillaviewmodel(){
   var self = this;
   self.ProductList = BugList.SoftwareProductList;
   self.priorityList = BugList.PriorityLevels;
   self.SoftProId = ko.observable();
   self.PriorityId = ko.observable();
} 

但不知道你的对象的结构我不能确定

【讨论】:

  • 我正在使用这个@Html.Raw(Json.Encode(Model));得到对象。该结构已经存在于顶部的问题中。
  • 因此,在您第一次选择的情况下,您的视图模型中无论如何都不存在变量“SoftProId”,“PriorityId”也是如此。将它们添加到视图模型中,代码@PaulManzotti 将正常工作。 Paul 在循环或子对象中不需要 $root 也是正确的。
  • “SoftProId”和“PriorityId”都是类的属性,您可以在我在问题控制器中创建的结构中看到它们。当我执行 @Html.Raw(Json.Encode(Model)); 时,我会得到相同的对象。所以两者都存在于 iewModel..
  • 您的视图模型是“bugzillaviewmodel”。这不是你的对象的完整定义吗?
  • 只需将 self.SoftProId = ko.observable()self.PriorityId = ko.observable() 添加到您的视图模型中
【解决方案3】:

你有两个问题:

  1. 您在 optiosnText 中写了“ProductList.ProductName”,而不仅仅是“ProductName”
  2. 对于值,它不是您所想的选项的值(html 值),而是将存储在视图模型中的变量中的值,例如“selectedProduct”,它将是可观察的

我认为这应该可行:

<div style="margin-top: 10px; width: 200px; float: left; font-weight: bold;">
    Products
    <select id="slSoftProducts" multiple="multiple" data-bind="options: ProductList, value:productSelected, optionsText: 'ProductName'">. </select> </div>
<div> style="margin-top: 10px; width: 200px; float: left; font-weight: bold;margin-left: 30px;">
priority Levels
    <select id="slPriorityLevels" multiple="multiple" data-bind="options: priorityList, value:prioritySelected, optionsText:'PriorityName'"></select>
</div>

js部分:

function bugzillaviewmodel(){
    var self = this;
    self.ProductList = BugList.SoftwareProductList;
    self.priorityList = BugList.PriorityLevels;                         
    self.productSelected = ko.observable(); // Will contain the selected product object from the ProductList
    self.prioritySelected = ko.observable(); // same but from the priorityList
}     

var viewModel = new bugzillaviewmodel();

// Knock Out Binding   through mapping.. 
//var viewModel = ko.mapping.fromJS(BugList);    
ko.applyBindings(viewModel);   

【讨论】:

    【解决方案4】:

    function bugzillaviewmodel(){
        var self = this;
        self.ProductList = BugList.SoftwareProductList;
        self.priorityList = BugList.PriorityLevels;  
        self.SelectedProductId = ko.observable();
        self.SelectedPriorityId = ko.observable();
    }     
    
    var viewModel = new bugzillaviewmodel();
    
    // Knock Out Binding   through mapping..  
    ko.applyBindings(viewModel);
    <div  style="margin-top: 10px; width: 200px; float: left; font-weight: bold;">
                Products
                <select id="slSoftProducts" multiple="multiple" data-bind="options: ProductList, optionsValue: SoftProId, optionsText: 'ProductName', value: SelectedProductId"></select>
            </div>
            <div style="margin-top: 10px; width: 200px; float: left; font-weight: bold; margin-left: 30px;">
                priority Levels
              <select id="slPriorityLevels" multiple="multiple" data-bind="options: priorityList, optionsValue: PriorityId, optionsText: 'PriorityName', value: SelectedPriorityId"></select>
    </div>

    【讨论】:

      【解决方案5】:

      试试这个...

      在服务器上,您的 Razor ViewModel 上有一个属性,该属性返回序列化对象或对象列表:

      public string SelectListToJson {
              get
              {
                  return JsonConvert.SerializeObject(YourSelectList);
              }
          }
      

      在视图中有这个:

      //DTO objects definition for mapping
      var SoftProduct = function(dto){
      	var self = this;
        self.ProductName = ko.observable(dto.ProductName);
        self.SoftProId = dto.SoftProId;
      };
      
      var Priority = function(dto){
      	var self = this;
        self.PriorityId = dto.PriorityId;
        self.PriorityName = ko.observable(dto.PriorityName);
      };
      
      //Output from Razor "@Html.Raw(Model)"
      //I.E. var BugList = @Html.Raw(Model)
      var BugList = {
      	SoftwareProductList: [
        	{ ProductName: "eCommerce Website", SoftProId: 1},
          { ProductName: "Banking Website", SoftProId: 2},
        ],
        PriorityLevels: [
        	{PriorityId: 1, PriorityName: "P1"},
          {PriorityId: 2, PriorityName: "P2"},
          {PriorityId: 3, PriorityName: "P3"},
        ]
      };
      
      //define main view model to apply bindings to
      var bugzillaviewmodel = function(){
          var self = this;
          self.ProductList = ko.mapping.fromJS([]);
          self.PriorityList = ko.mapping.fromJS([]);
          self.SelectedProducts = ko.observableArray();
          self.SelectedPriorities = ko.observableArray();
      };
      
      
        //init viewModel
        var viewModel = new bugzillaviewmodel();
      
        //map data in BugList to viewmodel
         ko.mapping.fromJS(
                         BugList.SoftwareProductList,
                         {
                             key: function (data) {
                                 return ko.utils.unwrapObservable(data.SoftProId);
                             },
                             create: function (options) {
                                 return new SoftProduct(options.data);
                             }
                         },
                         viewModel.ProductList);
         ko.mapping.fromJS(
                         BugList.PriorityLevels,
                         {
                             key: function (data) {
                                 return ko.utils.unwrapObservable(data.PriorityId);
                             },
                             create: function (options) {
                                 return new Priority(options.data);
                             }
                         },
                         viewModel.PriorityList);
      
        //apply bindings to dom                 
        ko.applyBindings(viewModel); 
      <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.0/knockout-min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout.mapping/2.4.1/knockout.mapping.min.js"></script>
      
      <div style="margin-bottom:20px;height:150px;">
        <div  style="margin-top: 10px; width: 200px; float: left; font-weight: bold;">
            Products<br/>
             <select id="slSoftProducts" multiple="true" data-bind="options: ProductList,optionsText: 'ProductName', optionsValue: 'SoftProId', selectedOptions: SelectedProducts"></select>   
        </div>
        <div style="margin-top: 10px; width: 200px; float: left; font-weight: bold; margin-left: 30px;">
             Priority Levels<br/>
             <select id="slPriorityLevels" multiple="true" data-bind="options: PriorityList, optionsText:'PriorityName', optionsValue:'PriorityId', selectedOptions: SelectedPriorities"></select>
        </div>
      </div>
      
      
      <div >
              <textarea rows="20" cols="100" data-bind="text: ko.toJSON($data, null, 2)"></textarea>
          </div>

      【讨论】:

        猜你喜欢
        • 2013-10-31
        • 2013-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-14
        • 1970-01-01
        • 2013-05-04
        • 2013-08-17
        相关资源
        最近更新 更多