【翻译】使用Knockout, Web API 和 ASP.Net Web Forms 进行简单数据绑定
In this post We are going to see How Knockout, ASP.Net Web API and ASP.Net works together smoothly. There are lots many examples of ASP.Net MVC,Web API and Knockout.js available on web working together nicely. So I thought it will be a good idea to write a blog post about how ASP.Net Web API, ASP.Net Web Forms,Knockout.js works together and how we can create simple data binding with Knockout.js.
这篇文章我们将见到如何使用 Knockout, ASP.Net Web API and ASP.Net 一起顺利工作。很多例子都是用 ASP.Net MVC, Web API 和 Knockout.js 一起很好地工作。所以我认为这是写关于 ASP.Net Web API, ASP.NET Web Forms, Knockout.js 一起工作并且我们可以使用Knockout.js创建简单数据绑定的博客的好主题
ASP.Net Web Forms:
ASP.Net Web 表单:
As we all know ASP.Net Web Forms is one of premier development technology widely use in creating web sites and web applications. ASP.Net Web Forms allow to create dynamic websites using event driven model. It is one of the easiest way to create web applications and web sites.
我们都知道家ASP.Net Web Forms 是一个建立网站及应用程序的重要开发技术。ASP.Net 允许使用事件驱动模型创建动态网站。它是一个用最简单的方法来创建应用程序和网站。
ASP.Net Web API:
ASP.Net Web API:
ASP.Net Web API is a framework that allows to build HTTP Service that reach a broad range of clients including browsers and mobile devices.It provides very easy way to write restful http services. Its can be access by any type of client over HTTP protocol. Client can make a GET, PUT,POST and DELETE request based on its requirement and get the response appropriately.
ASP.Net Web API 是一个允许建立HTTP服务广泛用于包括浏览器和移动设备的客户端的框架。它提供简单的方法编写RESTful HTTP服务。它可以被所有类型的HTTP 协议的客户端访问。客户端可以跟据需求制定一个GET,PUT,POST 和DELETE 请求并且得到适当的响应。
Knockout JS:
Knockout JS:
As per knockoutjs.com, Knockout is a JavaScript library that helps you to create rich, responsive display and editor user interfaces with a clean underlying data model. It provides a way to declarative bindings using an ‘Observable’ view model on browser. It supports two way bindings.
按照 knockoutjs.com说明,Knockout 是一个JavaScript类库,帮助你使用清晰的数据模型创建一个丰富的响应式显示和编辑用户界面。他提供一种声明’Observable’方法把视图模型绑定在浏览器上。
Getting Started with ASP.Net Web Forms and ASP.Net Web API:
开始使用ASP.Net Web Forms and ASP.Net API:
So let’s get started first we have to create an empty web forms application in Visual Studio 2012 via File->New Project Menu.
我们开始吧 首先我们在Visual Studio2012创建一个空的Web应用程序。步骤为文件->新建项目。

Now once application created we need to add reference to System.Web.Routing as we need to required routing for the ASP.Net Web API.
现在一旦创建应用程序,Web API路由功能要求我们需要添加 System.Web.Routing 引用。

Now It’s time to create a model class that we are going to use for Web API. Following a code for that class. It’s very basic as we are just creating sample demo. So I have created class with 3 properties.
现在是时候创建一个模型类型用于WebAPI.以下是这个类的代码。作为刚刚创建的示例这是很基本的。所以为这个类创建了3个属性。
namespace WebApplication1.App_Code
{
public class Employee
{
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
Now let’s add Web Api Controller Class to create a basic Web API.
现在,让我们为创建的Web API添加Api 控制器类。

Now it’s time to write a code for the our ASP.Net Web API Employee Controller and following code I have written for that. here I have created a list of employee and written that in GetEmployeee method.
现在是时候在ASP.Net Web API 员工控制器中写代码,以下是我写好的代码。
这里有我创建的员工清单和写好的GetEmployee方法。
using System;
using System.Collections.Generic;
using System.Web.Http; namespace WebApplication1.App_Code
{
public class EmployeeController : ApiController
{
public List<Employee> Employees = new List<Employee> {
new Employee {EmployeeId=,FirstName="Jalpesh",LastName="Vadgama"},
new Employee {EmployeeId=,FirstName="Vishal",LastName="Vadgama"},
new Employee {EmployeeId=,FirstName="Tushar",LastName="Maru"},
new Employee {EmployeeId=,FirstName="Himanshu",LastName="Patel"},
}; public IEnumerable<Employee> GetEmployees()
{
return Employees;
}
}
}
As now our Web API ready and we need to add a route for our API to handle a request. So let’s first add Global Application Class.
现在我们的Web API准备好了,并且我们需要添加API路由来处理请求。让我们首先添加全局应用程序类。

Now in application_start I have written following code to create a route for web api.
现在在application_Start里,我创建了Web API路由,代码如下。
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = System.Web.Http.RouteParameter.Optional }
);
}
Here I have just define route as api/controller so our URL will like this. http://localhost/api/employee
这里有刚才定义的 api/controller 路由。所以我们的URL将会象这样。http://localhost/api/employee
So now our API is almost ready. So It’s time to add scripts that we are going to use via NuGet package. We need to two script files jQuery for initial handling and making ajax calls and knockoutjs for bindings.
所以现在我们的API差不多装备好了。是时候通过NuGet包添加脚本了。我们需要两个JQuery脚本进得初始处理并且绑定AJAX调用和KnockoutJS。

same way for knockout.
Knockout也是同样的方法。

So we are ready to write a code calling API from JavaScript and perform binding for the knockout. So let’s first create a empty web form page and then we write some code in that.
所以我们准务写JavaScript 的API调用代码,并且执行Knockout绑定。让我们首先创建一个空白的Web表单和写一些代码。
Now first I have included both scripts in default.aspx head tag like following.
现在首先要包含两个脚本在Default.aspx中的Head标签中,像下面这样。
<head runat="server">
<script src="Scripts/jquery-2.0.0.min.js"></script>
<script src="Scripts/knockout-2.2.1.js"></script>
</head>
Then I have written following code for HTML
我编写好的HTML代码如下
<div>
<h1>Knockout demo with asp.net web forms and asp.net webapi</h1>
</div> <table >
<thead>
<tr>
<td>Employee Id</td>
<td>First Name</td>
<td>Last Name</td>
</tr>
</thead> <tbody data-bind="foreach: employees">
<tr >
<td data-bind="text: EmployeeId"></td>
<td data-bind="text: FirstName"></td>
<td data-bind="text: LastName"></td>
</tr>
</tbody>
</table>
Here you can see that I have create a simple HTML table where I have written data-bind attribute to some of the html tags. That is a way of knockout to tell this particular tag id find with text property of td and employee id property.
这里你会看到我创建的简单的HTML表格,在哪里有我写的一些数据绑定属性HTML标签
Then I have written following code for the java script.
然后我写入下面的 JavaScript代码。
var viewModel =
{
employees: ko.observableArray([]),
}
$(document).ready(function () {
$.ajax({
url: "/api/Employee",
contentType: "text/json",
type: "GET",
success: function (data) {
$.each(data, function (index) {
viewModel.employees.push(ToKnockOutObservable(data[index]));
});
ko.applyBindings(viewModel);
},
error: function (data) {
alert("error occured");
}
});
function ToKnockOutObservable(employee) {
return {
EmployeeId: ko.observable(employee.EmployeeId),
FirstName: ko.observable(employee.FirstName),
LastName: ko.observable(employee.LastName),
};
}
});
Here you can see that I have created a view model for employee in JavaScript which is necessary to bind html tags with view model with knockout.js. Here I have created employee as Observable arrays this is one of the ways of detecting changes in collection. You can find more information about at following link.
http://knockoutjs.com/documentation/observableArrays.html
这里你可以看到我创建的员工视图模型的JavaScript脚本,这里必须在Knockout.js使用视图模型进行HTML标签绑定。这里有我创建的用于观察的员工数组,这是在集合中检测变化的一种方法。你可以在下面链接中找到更多的信息。
http://knockoutjs.com/documentation/observableArrays.html
Then document.ready function I have made ajax call to web api and get a data for that and then create a collection of employee model with ToKnockOutObservable function. At the end I have used ko.Applybinding method to apply binding in HTML Tag.
[然后 document.ready 函数有我制造的AJAX调用Web API ,并且在ToKnockOutObservable函数中创建一个员工集合得到数据。]最后,我用ko.Applybinding 方法应用绑定HTML标签。
So we are done with coding let’s run this demo in browser like following. It’s works smoothly..
所以我们完成代码编写,让示例运行在浏览器中如下。它工作顺利..

That’s it. Hope you like it. Stay tuned for more..
到此为止。希望你喜欢。更多内容敬请期待..
【翻译】使用Knockout, Web API 和 ASP.Net Web Forms 进行简单数据绑定的更多相关文章
- Knockout, Web API 和 ASP.Net Web Forms 进行简单数据绑定
使用Knockout, Web API 和 ASP.Net Web Forms 进行简单数据绑定 原文地址:http://www.dotnetjalps.com/2013/05/Simple-da ...
- ASP.NET Web API和ASP.NET Web MVC中使用Ninject
ASP.NET Web API和ASP.NET Web MVC中使用Ninject 先附上源码下载地址 一.准备工作 1.新建一个名为MvcDemo的空解决方案 2.新建一个名为MvcDemo.Web ...
- Web API 2 入门——使用Web API与ASP.NET Web窗体(谷歌翻译)
在这篇文章中 概观 创建Web窗体项目 创建模型和控制器 添加路由信息 添加客户端AJAX 作者:Mike Wasson 虽然ASP.NET Web API与ASP.NET MVC打包在一起,但很容易 ...
- 在ASP.NET Web API和ASP.NET Web MVC中使用Ninject
先附上源码下载地址 一.准备工作 1.新建一个名为MvcDemo的空解决方案 2.新建一个名为MvcDemo.WebUI的空MVC应用程序 3.使用NuGet安装Ninject库 二.在ASP.N ...
- 【ASP.NET Web API教程】3 Web API客户端
原文:[ASP.NET Web API教程]3 Web API客户端 Chapter 3: Web API Clients 第3章 Web API客户端 本文引自:http://www.asp.net ...
- ASP.NET Web API路由系统:Web Host下的URL路由
ASP.NET Web API提供了一个独立于执行环境的抽象化的HTTP请求处理管道,而ASP.NET Web API自身的路由系统也不依赖于ASP.NET路由系统,所以它可以采用不同的寄宿方式运行于 ...
- Asp.Net Web API VS Asp.Net MVC
http://www.dotnet-tricks.com/Tutorial/webapi/Y95G050413-Difference-between-ASP.NET-MVC-and-ASP.NET-W ...
- 002.Create a web API with ASP.NET Core MVC and Visual Studio for Windows -- 【在windows上用vs与asp.net core mvc 创建一个 web api 程序】
Create a web API with ASP.NET Core MVC and Visual Studio for Windows 在windows上用vs与asp.net core mvc 创 ...
- Using MongoDB with Web API and ASP.NET Core
MongoDB is a NoSQL document-oriented database that allows you to define JSON based documents which a ...
随机推荐
- Unity3D占用内存太大的解决方法
原地址:http://www.cnblogs.com/88999660/archive/2013/03/15/2961663.html 最近网友通过网站搜索Unity3D在手机及其他平台下占用内存太大 ...
- HDOJ 1203 I NEED A OFFER!(01背包)
10397507 2014-03-25 23:30:21 Accepted 1203 0MS 480K 428 B C++ 泽泽 题目链接:http://acm.hdu.edu.cn/showprob ...
- 谷歌、百度、1万ip能赚多少钱?1000IP能够值多少钱呢?
谷歌.百度.1万ip能赚多少钱?1000IP能够值多少钱呢? (2014-04-03 11:50:52) 转载▼ 标签: 广告联盟 百度联盟 谷歌联盟 ip赚钱 很多在人问:谷歌.百度:1 ...
- 用nginx做反向代理来访问防外链图片
用nginx做反向代理来访问防外链图片 女儿的博客从新浪搬到wordpress后,发现原来博客上链接的新浪相册的图片都不能访问了,一年的博客内容,一个个去重新上传图片,修正链接也是个大工程.还是得先想 ...
- http://www.zhihu.com/question/24896283
http://www.zhihu.com/question/24896283 Rix Tox,太不專業了 三百.知乎用户.raintorr 等人赞同 1. 更改变量名的几种方法这种情况下该如何快速选中 ...
- linux下用mii-tool和ethtool 查看网线是否正确连接到网卡
输入mii-tool可以查看网线是否连接到网卡#mii-tool eth0: negotiated 100baseTx-FD, link ok 有时驱动可能不支持会出错下列错误#mii-tool SI ...
- AWS AutoScaling
origin_from: http://blog.csdn.net/libing_thinking/article/details/48327189 AutoScaling 是 AWS 比较核心的一个 ...
- JTA集成JOTM或Atomikos配置分布式事务(Tomcat应用服务器)
一.以下介绍Spring中直接集成JOTM提供JTA事务管理.将JOTM集成到Tomcat中. (经过测试JOTM在批量持久化时有BUG需要修改源码GenericPool类解决)! 参考文章http: ...
- Linux下RPM、tar.gz、DEB格式软件包的区别
初接解Linux的朋友一定对软件的安装特别头疼,同样都是for Linux,但RPM.tar.gz.DEB包还是有很大区别的,这种区别很有可能使你的安装过程进行不下去.那我们应该下载什么格式的包呢 ...
- MySQL 查询最大最小值优化
1. 假设你使用了Innodb存储引擎2. 假设你在innodb设定了主键(聚集索引) 3. 因为聚集索引页面之间是通过双向链表链接,页按照主键的顺序排序 每个页中的记录也是通过双向链表维护.聚集索引 ...