如何启用数据使用ASP.NET MVC 3英寸KnockoutJS结合;剃刀"查看引擎? [英] How to enable data binding in KnockoutJS using the ASP.NET MVC 3 "Razor" View Engine?

查看:94
本文介绍了如何启用数据使用ASP.NET MVC 3英寸KnockoutJS结合;剃刀"查看引擎?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实施<一个href=\"http://blog.stevensanderson.com/2010/07/12/editing-a-variable-length-list-knockout-style/\">this例如淘汰赛使用ASP MVC 3的剃刀视图引擎。

I'm trying to implement this Knockout example using ASP MVC 3's "Razor" view engine.

第一个主题涵盖简单的数据使用标准的ASP视图引擎一个C#阵列的结合。我试图用剃刀的样本为例,该行:

The first topic covers simple data binding of a C# array using the standard ASP view engine. I am trying the sample example using "Razor", and this line:

<script type="text/javascript"> 
    var initialData = <%= new JavaScriptSerializer().Serialize(Model) %>; 
</script>

导致一个空变量的 initialData

我也试过这样的:

@{
    string data = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Model);
}

然后指定的initialData是这样的:

And then specified the initialData like this:

var initialData = @Html.Raw(data);

本填充的 initialData 的与数据集,但结合不起作用。

This populates initialData with the dataset, but binding does not work.

我只是想以这种数据绑定设置才能显示的想法计数,如例如:

I'm just trying to databind this set in order to display a count of the ideas, as in the example:

<p>You have asked for <span data-bind="text: gifts().length">&nbsp;</span> gift(s)</p>

为什么没有数据绑定工作在这种情况?

Why isn't data binding working in this instance?

推荐答案

在MVC3最简单的方法就是做:

The easiest way in MVC3 is to do:

var initialData = @Html.Raw(Json.Encode(Model));

这篇关于如何启用数据使用ASP.NET MVC 3英寸KnockoutJS结合;剃刀&QUOT;查看引擎?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆