- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我到处搜索并尝试了各种模式,但似乎无法让它正常工作。有一次我让它工作时,有太多代码,我认为必须有一种更简单的方法,但现在它完全损坏了。我的目标...了解如何使用 KO 映射和 MVC 来加载网格的基础知识...并可能使用表单来更新它。我找不到任何适合我的情况的例子。这就是我现在的心情。
这是我的行动:
public JsonResult List()
{
var result = new JsonResult();
result.Data = _service.GetWeightStats();
return Json(result, JsonRequestBehavior.AllowGet);
}
这是我的类(class):
public class WeightStat
{
public int Id { get; set; }
[Required]
public double Weight { get; set; }
public double? Neck { get; set; }
public double? Chest { get; set; }
public double? Bicept { get; set; }
public double? Waist { get; set; }
public double? Hip { get; set; }
public double? Thigh { get; set; }
public double? Calf { get; set; }
[Required]
public DateTime Date { get; set; }
[Required]
public string TimeOfDay { get; set; }
[Required]
public string DietBehavior { get; set; }
}
这是我的模板行:
<tbody data-bind='template: {name: "statRowTemplate", foreach: data.Data }'></tbody>
这是我的模板:
<script id="statRowTemplate" type="text/html">
<tr data-bind="attr: { id: Id }">
<td><span data-bind="text: Date"></span></td>
<td><span data-bind="text: TimeOfDay"></span></td>
<td><span data-bind="text: DietBehavior"></span></td>
<td><span data-bind="text: Weight"></span></td>
<td><span data-bind="text: Neck"></span></td>
<td><span data-bind="text: Chest"></span></td>
<td><span data-bind="text: Bicept"></span></td>
<td><span data-bind="text: Waist"></span></td>
<td><span data-bind="text: Hip"></span></td>
<td><span data-bind="text: Thigh"></span></td>
<td><span data-bind="text: Calf"></span></td>
</tr>
</script>
这是我的 KO JS:
<script type="text/javascript">
var data = $.getJSON('WeightTracker/List');
var viewModel = ko.mapping.fromJSON(data);
ko.mapping.updateFromJSON(viewModel, data);
ko.applyBindings(viewModel);
从操作中返回 JSON:
{"ContentEncoding":null,"ContentType":null,"Data":[{"Id":1,"Weight":195.3,"Neck":10.3,"Chest":34.6,"Bicept":13.2,"Waist":34,"Hip":34,"Thigh":16.4,"Calf":8.8,"Date":"\/Date(1302554017067)\/","TimeOfDay":"Morning","DietBehavior":"Moderately"},{"Id":2,"Weight":194.3,"Neck":10.157142857142858,"Chest":34.4,"Bicept":13,"Waist":33.5,"Hip":33.5,"Thigh":16.2,"Calf":8.6000000000000014,"Date":"\/Date(1302640417070)\/","TimeOfDay":"Morning","DietBehavior":"Moderately"},{"Id":3,"Weight":193.3,"Neck":10.014285714285714,"Chest":34.2,"Bicept":12.799999999999999,"Waist":33,"Hip":33,"Thigh":15.999999999999998,"Calf":8.4,"Date":"\/Date(1302726817070)\/","TimeOfDay":"Morning","DietBehavior":"Moderately"},{"Id":4,"Weight":192.3,"Neck":9.8714285714285719,"Chest":34,"Bicept":12.6,"Waist":32.5,"Hip":32.5,"Thigh":15.799999999999999,"Calf":8.2000000000000011,"Date":"\/Date(1302813217070)\/","TimeOfDay":"Morning","DietBehavior":"Moderately"},{"Id":5,"Weight":191.3,"Neck":9.72857142857143,"Chest":33.800000000000004,"Bicept":12.399999999999999,"Waist":32,"Hip":32,"Thigh":15.599999999999998,"Calf":8,"Date":"\/Date(1302899617070)\/","TimeOfDay":"Morning","DietBehavior":"Moderately"},{"Id":6,"Weight":190.3,"Neck":9.5857142857142872,"Chest":33.6,"Bicept":12.2,"Waist":31.5,"Hip":31.5,"Thigh":15.399999999999999,"Calf":7.8000000000000007,"Date":"\/Date(1302986017070)\/","TimeOfDay":"Morning","DietBehavior":"Moderately"},{"Id":7,"Weight":189.3,"Neck":9.4428571428571431,"Chest":33.4,"Bicept":12,"Waist":31,"Hip":31,"Thigh":15.2,"Calf":7.6000000000000005,"Date":"\/Date(1303072417070)\/","TimeOfDay":"Morning","DietBehavior":"Moderately"},{"Id":8,"Weight":188.3,"Neck":9.3,"Chest":33.2,"Bicept":11.799999999999999,"Waist":30.5,"Hip":30.5,"Thigh":14.999999999999998,"Calf":7.4,"Date":"\/Date(1303158817070)\/","TimeOfDay":"Morning","DietBehavior":"Moderately"},{"Id":9,"Weight":187.3,"Neck":9.1571428571428584,"Chest":33,"Bicept":11.6,"Waist":30,"Hip":30,"Thigh":14.799999999999999,"Calf":7.2000000000000011,"Date":"\/Date(1303245217070)\/","TimeOfDay":"Morning","DietBehavior":"Moderately"},{"Id":10,"Weight":186.3,"Neck":9.0142857142857142,"Chest":32.800000000000004,"Bicept":11.399999999999999,"Waist":29.5,"Hip":29.5,"Thigh":14.599999999999998,"Calf":7.0000000000000009,"Date":"\/Date(1303331617070)\/","TimeOfDay":"Morning","DietBehavior":"Moderately"},{"Id":11,"Weight":185.3,"Neck":8.8714285714285719,"Chest":32.6,"Bicept":11.2,"Waist":29,"Hip":29,"Thigh":14.399999999999999,"Calf":6.8000000000000007,"Date":"\/Date(1303418017070)\/","TimeOfDay":"Morning","DietBehavior":"Moderately"},{"Id":12,"Weight":184.3,"Neck":8.72857142857143,"Chest":32.4,"Bicept":11,"Waist":28.5,"Hip":28.5,"Thigh":14.2,"Calf":6.6000000000000005,"Date":"\/Date(1303504417070)\/","TimeOfDay":"Morning","DietBehavior":"Moderately"},{"Id":13,"Weight":183.3,"Neck":8.5857142857142872,"Chest":32.2,"Bicept":10.799999999999999,"Waist":28,"Hip":28,"Thigh":13.999999999999998,"Calf":6.4,"Date":"\/Date(1303590817070)\/","TimeOfDay":"Morning","DietBehavior":"Moderately"},{"Id":14,"Weight":182.3,"Neck":8.4428571428571431,"Chest":32,"Bicept":10.6,"Waist":27.5,"Hip":27.5,"Thigh":13.799999999999999,"Calf":6.2000000000000011,"Date":"\/Date(1303677217070)\/","TimeOfDay":"Morning","DietBehavior":"Moderately"},{"Id":15,"Weight":181.3,"Neck":8.3,"Chest":31.8,"Bicept":10.399999999999999,"Waist":27,"Hip":27,"Thigh":13.599999999999998,"Calf":6.0000000000000009,"Date":"\/Date(1303763617070)\/","TimeOfDay":"Morning","DietBehavior":"Moderately"},{"Id":16,"Weight":180.3,"Neck":8.1571428571428584,"Chest":31.6,"Bicept":10.2,"Waist":26.5,"Hip":26.5,"Thigh":13.399999999999999,"Calf":5.8000000000000007,"Date":"\/Date(1303850017070)\/","TimeOfDay":"Morning","DietBehavior":"Moderately"},{"Id":17,"Weight":179.3,"Neck":8.014285714285716,"Chest":31.400000000000002,"Bicept":10,"Waist":26,"Hip":26,"Thigh":13.2,"Calf":5.6000000000000005,"Date":"\/Date(1303936417070)\/","TimeOfDay":"Morning","DietBehavior":"Moderately"},{"Id":18,"Weight":178.3,"Neck":7.8714285714285719,"Chest":31.200000000000003,"Bicept":9.7999999999999989,"Waist":25.5,"Hip":25.5,"Thigh":12.999999999999998,"Calf":5.4,"Date":"\/Date(1304022817070)\/","TimeOfDay":"Morning","DietBehavior":"Moderately"},{"Id":19,"Weight":177.3,"Neck":7.7285714285714295,"Chest":31,"Bicept":9.6,"Waist":25,"Hip":25,"Thigh":12.799999999999999,"Calf":5.2000000000000011,"Date":"\/Date(1304109217070)\/","TimeOfDay":"Morning","DietBehavior":"Moderately"},{"Id":20,"Weight":176.3,"Neck":7.5857142857142863,"Chest":30.8,"Bicept":9.3999999999999986,"Waist":24.5,"Hip":24.5,"Thigh":12.599999999999998,"Calf":5.0000000000000009,"Date":"\/Date(1304195617070)\/","TimeOfDay":"Morning","DietBehavior":"Moderately"}],"JsonRequestBehavior":1}
我现在假设我对这段代码完全错误,因为除了 JSON 字符串之外我没有收到任何返回的内容。我已经用很多不同的方式修改了代码,并且我已经为此工作了两个多星期,试图让它发挥作用,所以如果有人可以向我解释我做错了什么,而不是将我发送到knockoutjs网站示例,这些示例还没有很有帮助。预先感谢您的帮助,使我免于长出更多白发:/
PS。也添加到这个..我想使用一个 friend 创建的mvc扩展,我正在工作,但我必须做一些类似data.WeightStats之类的事情..很沮丧:/检查一下public static MvcHtmlString KnockoutForModel<TModel>(this AjaxHelper<TModel> helper, bool includeScriptTags = true, bool? applyKoBindings = null)
{
var serializer = new JavaScriptSerializer();
var json = "var viewModel = ko.mapping.fromJS(" + serializer.Serialize(helper.ViewData.Model) + ");\r\n";
if (applyKoBindings.GetValueOrDefault(includeScriptTags))
{
json += "ko.applyBindings(viewModel);\r\n";
}
if (includeScriptTags)
{
json = "<script type=\"text/javascript\">\r\n" + json + "</script>\r\n";
}
return MvcHtmlString.Create(json);
}
最佳答案
$.getJSON 要求您传递回调来处理结果,因为请求是异步完成的。所以,你会想做这样的事情:
var viewModel = {};
$.getJSON('WeightTracker/List', function(result) {
viewModel.stats = ko.mapping.fromJS(result.Data);
ko.applyBindings(viewModel);
});
然后,根据上面的内容,您的模板绑定(bind)将如下所示:
<tbody data-bind='template: {name: "statRowTemplate", foreach: stats }'></tbody>
因此,当您执行 $.getJSON 时,在回调中您将处理 JavaScript 对象而不是 JSON 字符串。
如果您有其他问题,我们很乐意为您提供帮助。
关于jquery - 使用 KnockoutJS 映射插件和 MVC 3 创建基本网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5837737/
如何将十进制数字转换为mixed radix表示法? 我猜想给定每个基数数组的输入和十进制数,它应该输出每列值的数组。 最佳答案 伪代码: bases = [24, 60, 60] input = 8
我有 Table-A,其中有“x”行。 (对于这个例子有 8 行) 我通过使用游标创建了列数为“x”的Table-C。 (使其动态化;如果将更多行添加到 Table-A,则会在 Table-C 中创建
我有一个关于对象的(很可能是简单而愚蠢的)问题。我创建了实例“Person”的对象“jon”。当我打电话时 console.log(jon.name) 控制台会给我输出“jon”。到目前为止,一切都很
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: javascript function vs. ( function() { … } ()); 抱歉,如果这太基础了
我正在尝试用 Java 重新创建射弹轨迹,但是,我遇到了一些问题。我看过很多解释公式之类的视频,但他们的方程中有一个目标,而我没有。我的意思是,他们有一个范围来计算子弹的下落,但我试图弄清楚子弹最终会
(希望如此)来自一个完整的 Rust 初学者的一个简单问题。我的循环有什么问题? num 计算结果为“69”的速度相当快,但是一旦 num 设置为“69”,循环就永远不会退出。我肯定遗漏了一些明显的东
我在 id="name"的元素上应用“.length”,但它计数为 29 而不是 14。我想知道我的错误在哪里?如果有人可以让我知道,那就太好了。谢谢! var name=document.getEl
我知道这很简单,但由于某种原因我无法让它工作。我正在尝试在 Java 中创建自定义颜色,但它似乎不起作用。 import java.awt.Color; Color deepGreen = new C
我有一个大文件,其中每一行都包含一个子字符串,例如 ABC123。如果我执行 grep ABC file.txt 或 grep ABC1 file.txt 我按预期返回这些行,但如果我执行 grep
我想将以下实体映射转换为 Priority 对象。在 getter 上,当我将“Short”更改为“Priority”并遵循 this.priority 时,它会提示 'basic' 属性类型不应该是
我正在开发一个相当基本的函数,我发现很难弄清楚为什么我会得到我的输出。 def mystery(n): print(n) if n < 4: my
我正在尝试对 WordPress 安装的新闻部分实现同位素过滤。我是 JavaScript/jQuery 的新手,正在尝试随时随地学习。我首先使用 Filters section of the Iso
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我在另一个实体类中引用一个实体并收到此错误。下面是示例代码。我在 persistence.xml 中也有这些类。 是什么导致了这个问题?我正在使用 Spring 数据 JPA 和 Hibernate。
我正在解析 HTML 并重新格式化图像以使其更好地适应。由于某种原因,当我有多个图像需要解析时,我会超出范围,而且我一生都无法弄清楚为什么。 当 imgArray.count >1 时,我将使用带有递
我是 SQL 新手,正在尝试创建一个基本的子查询。我需要找出经理的平均年龄和实习生的平均年龄之间的差异。 标题为一栏 - 经理或实习生年龄是一列,全部在同一个表中。 我会使用两个子查询来做类似的事情:
我习惯了 csh,所以不得不使用 bash 有点烦人。这段代码有什么问题? if[$time > 0300] && [$time 和 300 && time < 900 )) then mod
我建立了这个页面:http://excelwrestling.com/poola.php即将到来的双重锦标赛。我的大部分数据都是从我的 mySQL 数据库中提取的,现在只有一些示例数据。 我希望链接选
是否有任何原因导致以下内容不起作用: for (i=0;i < someArray.length;i++) { if (someArray[i].indexOf("something") !=
我现在正在学习 Javascript,有一个问题一直困扰着我! 因此,我在这里所需要做的就是在此输入框中键入颜色,单击按钮并将标题更改为键入的颜色(仅当键入的颜色位于变量中指定的数组中时)。 我的代码
我是一名优秀的程序员,十分优秀!