Ajax.NET Pro(AjaxPro)——Class Examples(类示例)

Class Examples

The first example will return an own class that has some public fields to be used on the client-side JavaScript.

以下的代码返回一个自定义类,包含一些公有权限的字段,这些字段将被客户端的JS代码使用。

public class MyClass
{
  public string FirstName = "";
  public string FamilyName = "";
  public int Age = 0;
}
PS:注意权限需要为public。

Click here to return an instance of the above class.

点击返回一个上例类的实例。

It is also working if you inherit from a class and add your own properties to the new class.

在集成其他的类,并在子类中添加属性的情况下,它仍然可以正常工作。

public class MyInheritedClass : MyClass
{
  public double SizeInMeters = 0.0;
  public Guid ID = Guid.Empty;
}
Click here to get an MyInheritedClass object.

点击获取一个继承自弗雷的子类的实例。

Passing a own class back to the server

发送一个自定义类给服务器。

Next we want to pass the MyClass object back to the server. The first call will get an MyClass object from the server like we have done above. Then we want to modify the .FirstName property on the client, submit the object to the server, modify the .FamilyName there and see the results.

接下来我们想将MyClass类对象发回给服务器端。第一个方法从服务器端获取一个MyClass类对象,如同上例所示。然后我们想要在客户端修改该实例的.FirstName属性值,然后将其(修改后的实例)提交给服务器端,修改属性值后提交然后我们来查看下结果。

function doTest3() {
  //异步调用服务器端方法来获取一个MyClass类的实例
  var p = AJAXDemo.Examples.Classes.Demo.GetMyClass().value;

  p.FirstName = "CLIENT-SIDE CHANGE";	//修改属性值
  AJAXDemo.Examples.Classes.Demo.PutMyClass(p, doTest3_callback);将该实例发送回服务器端
  p = null;
}
[AjaxMethod]
public MyClass PutMyClass(MyClass c)
{
  c.FamilyName = "SERVER-SIDE CHANGE";	// 修改属性
  return c;
}
Click here to run the test!

点击运行进行测试。

Create converters for your classes

为类创建转换器。

One new feature is the use of converters to serialize a .NET object or deserialize a JSON string. In this example I am using a custom IJavaScriptConverter. This converter will return a new class on the client-side JavaScript that may have more properties or methods that are not returned using the built-in custom object converter (which will only return public fields and properties).

新增了一个特性,转换器,用于序列化.NET对象或者序列化一个JSON字符串。在这个例子中,我使用了一个客户端的JS转换器。该转换器用于返回一个新的类,在客户端使用JS实现,该类包含了更多的属性和方法,这些额外的属性和方法通过内建的客户端对象转换器是无法返回的(它值返回公有public字段和属性)。

function doTest4() {
  var p = AJAXDemo.Examples.Classes.Demo.GetPerson().value;		// 异步调用服务器端方法
  // access the properties of the Person object here在这里访问Person类对象的属性值
  alert(p.FirstName);

  // Now, we want to save it, we call the save method of the instance
  // and get a boolean if succeded.
//然后,我们想要保存它。调用类对象的相同方法,并获取一个是否成功的boolean类型返回值。

Click here to get an Person instance and save the object using a method from the instance.

点击获取一个Person类的实例,并使用实例中的方法保存该对象。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值