Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将下拉列表中的默认选择指定给另一个模型中的模型类_C#_Javascript_Knockout.js_Webforms - Fatal编程技术网

C# 将下拉列表中的默认选择指定给另一个模型中的模型类

C# 将下拉列表中的默认选择指定给另一个模型中的模型类,c#,javascript,knockout.js,webforms,C#,Javascript,Knockout.js,Webforms,下面是一些相关的代码 加成 模型 我也有一个编辑功能设置与绑定。这可以正常工作,并且可以正确嵌套CertType模型,如下所示 "selectedCert": { "certId": 10, "certName": "AFC Service Training", "certCode": "AFCST", "description": "The training required to work AFC service", "certType": {

下面是一些相关的代码

加成 模型 我也有一个编辑功能设置与绑定。这可以正常工作,并且可以正确嵌套CertType模型,如下所示

  "selectedCert": {
    "certId": 10,
    "certName": "AFC Service Training",
    "certCode": "AFCST",
    "description": "The training required to work AFC service",
    "certType": {
      "certTypeId": 1,
      "certTypeName": "Certification"
    },
    "isEditing": false,
    "isValid": true
  },
然而,我已经尝试了一些组合,让我的addCert函数获取AvailableCertTypesObservableArray中的第一项并分配它。原因是,当我将其发布到我的web服务时,我需要确保在那里选择了某些内容

我不熟悉Knockout.js,所以甚至不知道什么是可能的,或者如何真正实现它,但理想情况下,当我调用addCert时,我希望看到类似的东西

"selectedCert": {
    //The first item in the observable array 
    "certType": {
      "certTypeId": 1,
      "certTypeName": "Certification"
    },
    "isEditing": false,
    "isValid": true
  }
但是,我却以

"selectedCert": {
    "certType": {},
    "isEditing": false,
    "isValid": true
  }
如果我需要发布更多的代码,请告诉我。非常感谢您的帮助

Edit Here是一个包含大部分标记的jsFiddle,尽管它不起作用

从web服务获取时,数组绑定正确,只是不确定如何硬编码其中的数组。目前只有这两个选项。

空证书类型问题的根源是由于设置不匹配/键入错误

 self.addCert = function () {
   self.selectedCert(new Certification(self.availableCertTypes()[0]));
 }
最终导致调用this.certType=ko.observatenew CertTypedata

修复此问题将从当前设置中产生预期结果

"selectedCertType":{"CertTypeId":1,"CertTypeName":"Certification"}
要获得所显示/请求的所需结果,请从选择绑定中删除选项value,并将该值绑定到with绑定提供的所选证书的certType


考虑事项:如果要使用现有证书中的数据填充select,则需要特别考虑。看看。

效果很好。除了嵌套新的CertificationCertTypeitem[index]之外,我还可以使用其他的赋值,这可能会让事情变得更简单一些吗?我觉得我现在的生活方式只会让我自己更难受。我想学习一些最佳实践,因为我只是在学习。这取决于你需要为其他活动保留多少信息。嵌套模型并没有固有的问题,您只需要了解所涉及的额外复杂性。根据我从小提琴上收集到的上下文,你现在对我来说是有意义的。如果您不需要certType对象来执行任何其他操作,在证书内部将其展平为id属性,并将数据绑定恢复为使用optionsValue:“CertTypeId”,这样您只存储id的简单属性,而不是完整对象。考虑到它是一个相当简单的对象,我可能只会将对象展平,而不是嵌套它。
"selectedCert": {
    "certType": {},
    "isEditing": false,
    "isValid": true
  }
 self.addCert = function () {
   self.selectedCert(new Certification(self.availableCertTypes()[0]));
 }
 var CertType = function (data) {
   this.certTypeId = ko.observable(data.CertTypeId); //expecting uppercase property
   this.certTypeName = ko.observable(data.CertTypeName);
 }

self.availableCertTypes = ko.observableArray([
        {
          "certTypeId": 1, //data-bind and this declaration are both lowercase               
          "certTypeName": "Certification"
        },
        {
          "certTypeId": 2,
          "certTypeName": "Training"
        }
    ]);
"selectedCertType":{"CertTypeId":1,"CertTypeName":"Certification"}
 <select id="CertificationType" 
         data-bind="options: $parent.availableCertTypes, 
                    optionsText: 'CertTypeName', 
                    value: certType"
         style="width:100%;"></select>
 "selectedCert":{"certType":{"CertTypeId":1,"CertTypeName":"Certification"},"isEditing":false,"isValid":true}