我只是試圖填充一對夫婦,我用jQuery渲染選擇標籤。我如何使用淘汰賽js和jquery填充選擇標籤?
從服務器傳入模式:
public class DepthChartsVm
{
public List<DepthChartModel> ReturnDepthCharts {get;set;}
}
public class DepthChartModel
{
public Team Team {get;set;}
public int SportId {get;set;}
}
public class Team
{
// various properties
public List<AthleteInfo> AthleteInfo {get;set;}
public List<positions> BasketBallPositonList {get;set;}
}
public class AthleteInfo
{
// various properties
List<DepthChart> DepthChartPositions {get;set;}
}
public class DepthChart
{
// various properties
prop ...etc
}
public class Positions
{
// various properties
prop ... etc.
}
HTML:
<div class="col-sm-4">
<label for="teamDropDown" class="text-center">Teams</label>
<select id="teamDropDown" data-bind="options: depthVm.ReturnDepthCharts, value: selectedTeam,
optionsText: function(item){return item.Team.FullName},
optionsCaption: 'select'"></select>
<p data-bind="if: selectedTeam">
<strong>Sport: </strong> <span data-bind="text: selectedTeam().Team.Sport"></span><br />
<strong>Season: </strong> <span data-bind="text: selectedTeam().Team.Season"></span>
</p>
</div>
<!--Athletes-->
<div class="col-sm-4">
<div data-bind="if: selectedTeam">
<h3 class="text-center">Athletes</h3>
<div data-bind="foreach: selectedTeam().Team.AthleteInfo">
<p>
<strong>Name: </strong><span data-bind="text: FirstName()
+ ' ' + LastName()"></span>
</p>
<div data-bind="foreach: DepthChartPositions()">
<p>
<input class="hidden" name="InputUpdateDepthChart.RosterId"
data-bind="value: RosterId"/>
Position: <select name="InputUpdateDepthChart.PositionId"
data-bind="options: $root.selectedTeam().Team.BasketBallPostionList,
optionsText: 'Position', value: PositionId, optionsValue: 'Id'"></select>
String: <select name="InputUpdateDepthChart.StringId"
data-bind="options: DepthStrings, optionsText: 'String',
value: StringId, optionsValue: 'Id'"></select>
</p>
<p>
<button data-bind="click: $root.addPosition">Add Position</button>
</p>
</div>
</div>
</div>
</div>
</div>
// script that serializes the model and passes it to knockout
<script>
// json encode incoming model
var depthModelData = @Html.Raw(JsonConvert.SerializeObject(Model))
// update depth chart vM
ko.applyBindings(new UpdateDepthChartVm(depthModelData),
document.getElementById('updateDepthChartForm'));
</script>
視圖模型:
var UpdateDepthChartVm = function (model) {
var self = this;
self.depthVm = ko.mapping.fromJS(model);
// selected team
self.selectedTeam = ko.observable();
// add position
self.addPosition = function() {
self.selectedTeam.Team.AthleteInfo.DepthChartPositions.push(new depthChartPosition());
};
function depthChartPosition(values) {
values = values || {};
var model = {
RosterId: ko.observable(values.RosterId),
PositionId: ko.observable(values.PositionId),
StringId: ko.observable(values.StringId),
};
return model;
}
};
基本上我想要當用戶希望增加另一個位置複製在飛行的第一段。 jquery確實在使用select標籤呈現新段落方面起作用,但是select字段是空的,因爲看起來它們和我的視圖模型之間沒有綁定。除此之外,我的視圖模型正在工作。我在這裏錯過了什麼?
無論出於何種原因,SO不會讓我對我以前的答案進行編輯,所以我必須提交一個新的。抱歉耽擱了。我希望編輯提交錯誤將被整理出來。 –
好吧,現在我感覺特別密集。所以我最初理解了映射的概念,但我對如何將其實際應用到視圖模型感到困惑。我的意思是因爲有幾個屬性需要映射,我必須爲每個映射調用ko.mapping嗎?例如,在上面的代碼中,您有self.depthVm = ko.mapping.fromJs(model,modelMappings); modelMappings在哪裏定義?或者我必須一遍又一遍地調用該語句,並用每個單獨定義的映射替換modelMapping? – user1206480
對不起modelMappings是一個錯誤。它現在糾正了。同樣,ko映射不會遞歸探索您的模型以在每個級別創建可觀察屬性。相反,只有提供模型的直接子項的屬性纔是可觀察的。這很有意義嗎? –