Jquery 使用knockoutjs在视图中显示对象
我需要帮助,我有返回对象的操作(JSON格式,我使用以下方法:http://code.msdn.microsoft.com/Build-truly-RESTful-API-194a6253“”,这是我从操作返回的对象(DashboardController的索引): 首先,我想显示我的模型的BuzzLeaderCompaigns(它是Compaign的一个ICollection),下面是我的视图:Jquery 使用knockoutjs在视图中显示对象,jquery,json,asp.net-mvc-3,knockout.js,Jquery,Json,Asp.net Mvc 3,Knockout.js,我需要帮助,我有返回对象的操作(JSON格式,我使用以下方法:http://code.msdn.microsoft.com/Build-truly-RESTful-API-194a6253“”,这是我从操作返回的对象(DashboardController的索引): 首先,我想显示我的模型的BuzzLeaderCompaigns(它是Compaign的一个ICollection),下面是我的视图: <h3>My Compaign</h3>
<h3>My Compaign</h3>
<table>
<thead>
<tr>
<th>Compaign Name</th><th>Compaign Description</th><th>End Date</th>
</tr>
</thead>
<tbody data-bind="foreach: BuzzLeaderCompaigns">
<tr>
<td data-bind="text: Name" ></td>
<td data-bind="text: Description"></td>
<td data-bind="text: EndDate"></td>
</tr>
</tbody>
</table>
<script type="text/javascript">
function Compaign(data) {
this.BuzzCompaignId = ko.observable(data.BuzzCompaignId);
this.Name = ko.observable(data.Name);
this.Description = ko.observable(data.Description);
this.EndDate = ko.observable(data.EndDate);
}
function DashboardViewModel() {
var self = this;
self.BuzzLeaderCompaigns = ko.observableArray([]);
self.CurrentCompaigns = ko.observableArray([]);
self.BuzzCompaignByInterest = ko.observableArray([]);
// Load initial state from server, convert it to Task instances, then populate self.tasks
$.getJSON("/Dashboard/Index", function (Data) {
var mappedData = $.map(Data, function() { return } ) ;
});
}
ko.applyBindings(new DashboardViewModel());
</script>
我的公司
公司名称公司名称公司名称和日期
功能比较(数据){
this.BuzzCompaignId=ko.可观察(data.BuzzCompaignId);
this.Name=ko.observable(data.Name);
此.Description=ko.可观察(data.Description);
this.EndDate=ko.可观察(data.EndDate);
}
函数DashboardViewModel(){
var self=这个;
self.BuzzLeaderCompaigns=ko.observearray([]);
self.CurrentCompaigns=ko.observearray([]);
self.buzzcompignbyinterest=ko.observearray([]);
//从服务器加载初始状态,将其转换为任务实例,然后填充self.tasks
$.getJSON(“/Dashboard/Index”),函数(数据){
var mappedData=$.map(数据,函数(){return});
});
}
ko.applyBindings(新的DashboardViewModel());
如何将数据绑定到我的viewmodel(当我使用$.getJSON获取数据时),然后再绑定到我的视图
- 如果您的视图(cshtml)被称为“索引”,那么您应该在控制器上创建一个不同的操作来返回json
- 而不是返回
您应该让新操作返回视图(模型)
使操作返回JSON(而不是HTML)。现有操作(索引)应返回无模型视图,即Json(model,JsonRequestBehavior.AllowGet)
返回视图()代码>
- 更改
的路径以使用新路径,即: $.getJSON(“/Dashboard/GetCampaign”,函数(数据){ 填充viewmodel的代码 ))getJSON
<script type="text/javascript">
// this example is using Json.Net
var model = @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model, new Newtonsoft.Json.Converters.IsoDateTimeConverter()));
// wrapper contains additional methods and logic
// knockout mapping plugin is used to convert json objects to observable objects
var wrapper = new ViewModelWrapper(ko.mapping.fromJS(model));
ko.applyBindings(wrapper);
</script>
//这个例子使用的是Json.Net
var model=@Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(model,new Newtonsoft.Json.Converters.IsoDateTimeConverter());
//包装器包含附加的方法和逻辑
//敲除映射插件用于将json对象转换为可观察对象
var wrapper=newviewmodelwrapper(ko.mapping.fromJS(model));
ko.applyBindings(包装纸);
假设/Dashboard/Index返回当前的compigns,请更改此选项:
$.getJSON("/Dashboard/Index", function (Data) {
var mappedData = $.map(Data, function() { return } ) ;
});
为此:
$.getJSON("/Dashboard/Index", function (Data) {
var mappedData = $.map(Data, function(item) { return new Compaign(item) });
self.CurrentCompaigns(mappedData);
});
你应该考虑仲裁器的建议;但是,仪表板/索引听起来像是一个应该返回视图的动作方法,而不是JSON动作方法。你可以有三个单独的JSON方法用于你的三个SabababyRead,或者你可以有一个返回所有“CopaIGIN”的方法。然后使用ko.utils.arrayFilter对三个数组进行筛选和赋值。
索引操作可以返回JSON,我使用这个api:然后索引()上有注释([EnableJson]);$.getJSON("/Dashboard/Index", function (Data) {
var mappedData = $.map(Data, function(item) { return new Compaign(item) });
self.CurrentCompaigns(mappedData);
});