[英]how map model to knockout
How I can map array from my model to knockout viewModel? 我如何将数组从模型映射到剔除viewModel?
I have tried this: 我已经试过了:
@using Newtonsoft.Json
@model Hotel.Web.Controllers.FoodOrderModelView
<table class="table table-responsive table-hover" data-bind="foreach: Items">
<tr class="row">
<td>
<label data-bind="text: $data"></label>
<label data-bind="text: Name"></label>
</td>
</tr>
</table>
<script>
function viewModel() {
self = this;
self.Items = ko.observableArray([]);
var jsonModel = @Html.Raw(JsonConvert.SerializeObject(Model.LunchItems, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore}));
console.log(jsonModel);
var mvcModel = ko.mapping.fromJS(jsonModel,{}, self);
self.Items(mvcModel);
console.log(self.Items);
};
$(function () {
var myViewModel = new viewModel();
ko.applyBindings(myViewModel);
});
</script>
I get right JSON and strange message. 我得到正确的JSON和奇怪的消息。
On console I get this log: 在控制台上,我得到以下日志:
why I can't see mvcModel? 为什么我看不到mvcModel?
1. [Object, Object, ...]
0:
Object Count: 0
Name: " item"
...
2. function observable() {
if (arguments.length > 0) {
// Write
// Ignore writes if the value hasn't changed
if (observable.isDifferent(observable[observableL…
EDITED: 编辑:
JSON: JSON:
[
{
"LunchOrderItemId": 0,
"FoodItem": {
"LunchItemID": 1,
"Name": " item",
"Price": 196,
"Description": " item description",
"Category": 2,
"File": null,
"FileId": 1
},
"FoodItemId": 0,
"Count": 0
},
{
"LunchOrderItemId": 0,
"FoodItem": {
"LunchItemID": 2,
"Name": "1 item",
"Price": 29,
"Description": "1 item description",
"Category": 2,
"File": null,
"FileId": null
},
"FoodItemId": 0,
"Count": 0
},
{
"LunchOrderItemId": 0,
"FoodItem": {
"LunchItemID": 3,
"Name": "2 item",
"Price": 19,
"Description": "2 item description",
"Category": 2,
"File": null,
"FileId": null
},
"FoodItemId": 0,
"Count": 0
}
]
Here fiddler: https://jsfiddle.net/ez358nv6/ 这里的提琴手: https : //jsfiddle.net/ez358nv6/
Try to parse the JSON to JS object first with JSON.parse(jsonModel)
, See how to parse the json to js object in the snippet: 尝试首先使用JSON.parse(jsonModel)
将JSON解析为JS对象,请参阅如何在代码段中将JSON解析为JS对象:
var jsonModel = '[{"LunchOrderItemId":0,"FoodItem":{"LunchItemID":1,"Name":" item","Price":196.00,"Description":" item description","Category":2,"File":null,"FileId":1},"FoodItemId":0,"Count":0},{"LunchOrderItemId":0,"FoodItem":{"LunchItemID":2,"Name":"1 item","Price":29.00,"Description":"1 item description","Category":2,"File":null,"FileId":null},"FoodItemId":0,"Count":0},{"LunchOrderItemId":0,"FoodItem":{"LunchItemID":3,"Name":"2 item","Price":19.00,"Description":"2 item description","Category":2,"File":null,"FileId":null},"FoodItemId":0,"Count":0}]'; function viewModel() { self = this; self.Items = ko.mapping.fromJS([]); ko.mapping.fromJS(JSON.parse(jsonModel),self.Items); console.log(self.Items()); }; $(function() { var myViewModel = new viewModel(); ko.applyBindings(myViewModel); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout.mapping/2.4.1/knockout.mapping.min.js"></script> <table class="table table-responsive table-hover" data-bind="foreach: Items"> <tr class="row"> <td> <label data-bind="text: $data.FoodItem.Name"></label> <label data-bind="text: $data.FoodItem.Description"></label> </td> </tr> </table>
But this is a workaround because you are using JSON.Net to Serialize the model and it return a JSON string, you can use this to raw your model
@Html.Raw(Json.Encode(Model))
and it will be the JS object from the JSON. 但这是一种解决方法,因为您正在使用JSON.Net来序列化模型,并且它返回JSON字符串,您可以使用它来获取模型@Html.Raw(Json.Encode(Model))
,它将成为JS对象。从JSON。
You have all sorts of problems in your code, most notably: 您的代码中有各种各样的问题,最明显的是:
fromJS
as well as supplying self
as the third argument (ie the update target). 您既要保存 fromJS
的结果, fromJS
提供self
作为第三个参数(即更新目标)。 From what I see the latter should suffice. 从我看来,后者就足够了。 self
properly as a var
. 你不是宣称self
正确的var
。 fromJS
to apply it to self
as a target, the JSON should be an object with an Items
property. 您的JSON 是一个数组,但是如果您想使用fromJS
将其应用到self
作为目标,则JSON应该是具有Items
属性的对象。 Items
have a Name
property, but according to your own JSON that's nested one level deeper. 您的视图假定所有Items
都具有Name
属性,但是根据您自己的JSON嵌套了一层。 By the way, you should have Items
as a property on the thing going into fromJS
. 顺便说一句,您应该将Items
作为进入fromJS
的东西的属性。 You can either fix that on the c# side, or you can hack around it as below in my example. 您可以在c#端修复该问题,也可以按以下示例中的方法进行破解。
With those things fixed, here's a slightly improved code sample to work with: 修复了这些问题之后,可以使用下面的代码示例进行一些改进:
// Abbreviated var rawJson = [ { "LunchOrderItemId": 0, "FoodItem": { "LunchItemID": 1, "Name": " item" } }, { "LunchOrderItemId": 0, "FoodItem": { "LunchItemID": 2, "Name": "1 item" } } ]; function viewModel() { var self = this; self.Items = ko.observableArray([]); var jsonModel = { Items: rawJson }; ko.mapping.fromJS(jsonModel, {}, self); }; var myViewModel = new viewModel(); ko.applyBindings(myViewModel);
label { padding: 4px; border: 1px solid gray; display: inline-block; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.0/knockout-min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout.mapping/2.4.1/knockout.mapping.min.js"></script> <table data-bind="foreach: Items"> <tr> <td> <label data-bind="text: LunchOrderItemId"></label> <label data-bind="text: FoodItem.Name"></label> </td> </tr> </table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.