【问题标题】:Select List from List in ViewModel presenting options as "[object Window]"从 ViewModel 中的列表中选择列表,将选项显示为“[对象窗口]”
【发布时间】:2015-05-25 09:49:01
【问题描述】:

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

我的视图模型如下所示:

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(data) 按要求:

{"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>

您可以看到,它没有显示客户,而是为所有客户显示“[object Window]”。我认为它可能是一半,因为它显示了正确的“[object Window]”数量与数据库中的客户端数量。

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

任何帮助将不胜感激。

【问题讨论】:

  • 客户类中有哪些属性?
  • 现在只需:int CustomerId {get; set;} 和 string CustomerName { get; set; }
  • 当您从服务器获取值时,客户会收到什么?它可能类似于 data.Customers。我向您询问了客户类的属性,因为当来自服务器的属性名称与 Select 中的附加值不匹配时会出现对象错误。
  • 在将视图模型返回到视图之前,我从存储库中填充了控制器中的客户。所以实际上视图模型包含当前的销售订单、所有它的子销售订单实体和客户集合。它不能是 data.Customers,因为我们在绑定到选择列表时不再在 js 函数中,所以此时数据是未定义的。
  • 所以 dio 你的意思是客户是来自 ko.applyBindings(salesOrderViewModel) 的 salesOrderViewModel 的一部分;

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


【解决方案1】:

您是不是在选项文本和选项值中缺少“”?

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

【讨论】:

  • 谢谢。你的答案让我成功了 80%,但在问题的背景下,我想这是正确的答案。您的回答按要求显示了客户,但与 salesOrderViewModel 的绑定不起作用。 data-bind 中的Value 应该是salesOrderViewMode.CustomerId,而不仅仅是CustomerId。谢谢您的帮助。完成:&lt;select class="form-control" name="Customers" id="Customers" data-bind="options: Customers, optionsText: 'CustomerName', optionsValue: 'CustomerId', value: salesOrderViewModel.CustomerId, event: {change: flagSalesOrderAsEdited}"&gt;&lt;/select&gt;
【解决方案2】:

您需要在绑定客户选择下拉菜单时进行一项更改

您的现有代码

<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

它应该适合你。

【讨论】:

  • 谢谢 Dnyanesh,这没用。我按照建议进行了更改,并且页面现在在 Firefox 中没有完全加载,并且 js 控制台显示错误 ReferenceError: CustomerName is not defined
  • 恐怕这也行不通。虽然现在选择列表现在有一个 [object Object] 集合,而不是之前的集合 [object Window]。不过,我感谢您提供帮助。
猜你喜欢
  • 2016-07-29
  • 1970-01-01
  • 1970-01-01
  • 2012-10-15
  • 1970-01-01
  • 1970-01-01
  • 2013-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多