当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - knockout options binding controls for more than one model

问题描述:

I work in this tutorial

http://www.asp.net/web-api/overview/creating-web-apis/using-web-api-with-entity-framework/part-9

I want to generate edit form where the author is drop down list and the current author is selected I use a form similar to this

function getAllbooks() {

ajaxHelper(baseUri, 'GET').done(function (data) {

self.books(data);

});

<ul id="update-book" data-bind="foreach: books">

<li>

<div>

<div class="item">Book ID</div> <span data-bind="text: $data.ID"></span>

</div>

<div class="form-group">

<label for="inputAuthor" class="col-sm-2 control-label">Author</label>

<div class="col-sm-10">

<select data-bind="options:authors, optionsText: 'Name', value: $data.AuthorId"></select>

</div>

</div>

<div>

<div class="item">Genre</div>

<input type="text" data-bind="value: $data.Genre"/>

</div>

<div>

<div class="item">Price</div>

<input type="text" data-bind="value: $data.Price"/>

</div>

<div>

<div class="item">Title</div>

<input type="text" data-bind="value: $data.Title"/>

</div>

<div>

<div class="item">Year</div>

<input type="text" data-bind="value: $data.Year"/>

</div>

<div>

<input type="button" value="Update" data-bind="click: $root.update"/>

<input type="button" value="Delete Item" data-bind="click: $root.remove"/>

</div>

</li>

</ul>

</div>

but I have javascript error authors undefined because it is inside the foreach book and I am not sure when I used value: $data.AuthorId will give me the id or not

网友答案:

I can only guess your data structure, if the author object has a Id,

You should do

<select data-bind="options: $root.authors,
  optionsText: 'Name',
  optionsValue: 'Id',
  value: AuthorId"></select>

But there is another problem, if your book.AuthorId is not a ko.observable (this is my guess from your code), the above binding would not update the value of it when user selects different author.

分享给朋友:
您可能感兴趣的文章:
随机阅读: