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類的實例,並使用實例中的方法保存該對象。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章