从ViewModel中的List中选择List,将选项显示为" [object Window]"

时间:2015-03-22 09:19:40

标签: c# asp.net-mvc knockout.js

我想在我的视图上创建一个选择列表,允许客户从该选择列表中选择客户。

我的视图模型如下所示:

public int SalesOrderId { get; set; }
public int CustomerId { get; set; }
public string PONumber { get; set; }
public DateTime OrderDate { get; set; }

public List<Customer> Customers { get; set; }
public List<SalesOrderItemViewModel> SalesOrderItems { get; set; }

我的客户模型如下所示:

public int CustomerId { get; set; }
public string CustomerName { get; set; }

我的淘汰赛js看起来像这样:

SalesOrderViewModel = function (data) {
    var self = this;
    // This automatically creates my view model properties for ko from my view model passed from the server
    ko.mapping.fromJS(data, salesOrderItemMapping, self);

    // .... Other save functions etc.
};

[编辑] 按要求销售物料映射

var salesOrderItemMapping = {
    'SalesOrderItems': {
        // key for each child
        key: function (salesOrderItem) {
            return ko.utils.unwrapObservable(salesOrderItem.SalesOrderItemId);
        },
        // Tell knockout what to fo for each sales order item it needs to create
        create: function (options) {
            // create a new sales order item view model using the model passed to the mapping definition
            return new SalesOrderItemViewModel(options.data); // Notice data is a property of the options object
            // Moreover, data is the data for this child and options is the object containing the parent
        }
    }
};

[编辑] @ Html.Raw(数据)按要求:

{"SalesOrderId":1,"CustomerId":1,"PONumber":"123456","OrderDate":"2015-01-25T00:00:00","MessageToClient":"The original value of Customer Name is Ashfield Clutch Services.","ObjectState":0,"Customer":{"CustomerId":1,"CustomerName":"Ashfield Clutch Services"},"SalesOrderItems":[{"SalesOrderItemId":1,"ProductCode":"ABC","Quantity":10,"UnitPrice":1.23,"SalesOrderId":1,"ObjectState":0},{"SalesOrderItemId":2,"ProductCode":"XYZ","Quantity":7,"UnitPrice":14.57,"SalesOrderId":1,"ObjectState":0},{"SalesOrderItemId":3,"ProductCode":"SAMPLE","Quantity":3,"UnitPrice":15.00,"SalesOrderId":1,"ObjectState":0}],"SalesOrderItemsToDelete":[],"Customers":[{"CustomerId":1,"CustomerName":"Ashfield Clutch Services"},{"CustomerId":3,"CustomerName":"Davcom-IT Ltd"},{"CustomerId":2,"CustomerName":"Fluid Air Conditioning"}]} 

在我看来,我有这个js:

<script src="~/Scripts/salesOrderViewModel.js"></script>
<script type="text/javascript">
    var salesOrderViewModel = new SalesOrderViewModel(@Html.Raw(data));
    ko.applyBindings(salesOrderViewModel);
</script>

如果我将选择列表绑定到视图模型中的Customers集合,则选择列表似乎没有正确绑定:

<select class="form-control" name="Customers" id="Customers" data-bind="options: Customers, optionsText: CustomerName, optionsValue: CustomerId, value: CustomerId"></select>

screenshot

您可以看到,而不是向客户显示&#34; [object Window]&#34;为所有客户。我认为它可能只有一半,因为它显示了正确的数字&#34; [object Window]&#34; vs db中的客户端数量。

我知道客户必须是淘汰可观察阵列,但我不确定如何将其应用于我的js视图模型。

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:1)

你不想错过&#34;&#34;在选项文本和选项值?

<select class="form-control" name="Customers" id="Customers" data-bind="options: Customers, optionsText: "CustomerName", optionsValue: "CustomerId", value: CustomerId"></select>

答案 1 :(得分:0)

您需要在绑定客户时进行一次更改以选择下拉列表

您的现有代码

<select class="form-control" name="Customers" id="Customers" data-bind="options: Customers, optionsText: CustomerName, optionsValue: CustomerId, value: CustomerId"></select>

进行以下更改

<select class="form-control" name="Customers" id="Customers" data-bind="options: salesOrderViewModel.Customers, optionsText: Customers.CustomerName, optionsValue: Customers.CustomerId, value: CustomerId"></select>
  

选项:salesOrderViewModel.Customers

它应该适合你。