如何使用 ASP.NET MVC 3 “Razor"在 KnockoutJS 中启用数据绑定查看引擎? [英] How to enable data binding in KnockoutJS using the ASP.NET MVC 3 "Razor" View Engine?

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

问题描述

我正在尝试实施这个淘汰赛示例 使用 ASP MVC 3 的Razor"视图引擎.

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

第一个主题涵盖使用标准 ASP 视图引擎的 C# 数组的简单数据绑定.我正在尝试使用Razor"的示例示例,这一行:

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 “Razor"在 KnockoutJS 中启用数据绑定查看引擎?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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